P
P
pvshvb2021-01-27 14:30:54
PHP
pvshvb, 2021-01-27 14:30:54

How to correctly form a CURL request with a proxy?

Good afternoon, I need to parse ozone through php on a system basis, but I don't know CURL well, especially when using a proxy. Can you please write the simplest example of a curl request using a proxy :)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nadim Zakirov, 2021-01-27
@pvshvb

An example of a request through an http proxy with browser imitation:

<?php

// Указываем тип документа и кодировку:
header('Content-Type: text/html; charset=utf-8');

// Включаем отображение ошибок:

ini_set('error_reporting', E_ALL);
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);

// Адрес для парсинга:
$url = 'https://yousite.com';

$proxy = '45.137.189.251:65233'; // Прокси вместе с портом
$proxyauth = 'login:password'; // Логин и пароль от прокси

// Создаём новый сеанс:
$curl = curl_init();

// Указываем адрес целевой страницы:
curl_setopt($curl, CURLOPT_URL, $url);

// О отключаем проверку SSL сертификата:
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);

// Указываем настройки нашего прокси:
curl_setopt($curl, CURLOPT_PROXY, $proxy);
curl_setopt($curl, CURLOPT_PROXYUSERPWD, $proxyauth);

// Указываем тип прокси:
curl_setopt($curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);

// Устанавливаем заголовки для имитации браузера:

$headers = [];
$headers[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9';
$headers[] = 'Accept-Encoding: identity';
$headers[] = 'Accept-Language: ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7';
$headers[] = 'Cache-Control: no-cache';
$headers[] = 'Connection: keep-alive';
$headers[] = 'Host: ' . parse_url($url)['host'];
$headers[] = 'Pragma: no-cache';
$headers[] = 'Sec-Fetch-Dest: document';
$headers[] = 'Sec-Fetch-Mode: navigate';
$headers[] = 'Sec-Fetch-Site: none';
$headers[] = 'Sec-Fetch-User: ?1';
$headers[] = 'Upgrade-Insecure-Requests: 1';
$headers[] = 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.193 Safari/537.36';


curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

// Разрешаем переадресацию:
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);

// Запрещаем прямой вывод результата запроса:
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

// Делаем сам запрос:
$result = curl_exec($curl);

// Завершаем сеанс:
curl_close($curl);

// Смотрим результат:
echo $result;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question