CURL bir proxy üzerinden nasıl kullanılır?

curl'ü bir proxy sunucusu kullanacak şekilde ayarlamak istiyorum. URL bir html formu tarafından sağlanıyor ve bu bir sorun teşkil etmiyor. Proxy olmadan gayet iyi çalışıyor. Bu ve diğer sitelerde kod buldum, ancak çalışmıyorlar. Doğru çözümü bulmak için herhangi bir yardım çok takdir edilecektir. Feryatların yakın olduğunu, ancak bir şeyleri kaçırdığımı hissediyorum. Teşekkür ederim.

Buradan uyarladığım aşağıdaki kod http://www.webmasterworld.com/forum88/10572.htm ancak 12. satırda eksik bir T_VARIABLE ile ilgili bir hata mesajı döndürüyor.

<?

$url = '$_POST[1]';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 0);
curl_setopt($ch, CURLOPT_PROXY, '66.96.200.39:80');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,'GET');
curl_setopt ($ch, CURLOPT_HEADER, 1)
curl_exec ($ch); 
$curl_info = curl_getinfo($ch);
curl_close($ch);
echo '<br />';
print_r($curl_info);
?>

Körük https://stackoverflow.com/questions/4802816/curl-through-proxy-returns-no-content adresinden alınmıştır.

<?

$proxy = "66.96.200.39:80";
$proxy = explode(':', $proxy);
$url = "$_POST[1]";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXY, $proxy[0]);
curl_setopt($ch, CURLOPT_PROXYPORT, $proxy[1]);
curl_setopt($ch, CURLOPT_HEADER, 1);

$exec = curl_exec($ch);

echo curl_error($ch);
print_r(curl_getinfo($ch));
echo $exec;
?>

şu anda pelican-cement.com'da yayında ancak o da çalışmıyor.

GÜNCELLEME: Yardımlarınız için teşekkür ederim, yukarıdaki değişiklikleri yaptım. Şimdi sadece boş bir ekran döndürüyor.

<?

$url = $_POST['1'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 0);
curl_setopt($ch, CURLOPT_PROXY, '66.96.200.39:80');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,'GET');
curl_setopt ($ch, CURLOPT_HEADER, 1);
curl_exec ($ch); 
$curl_scraped_page = curl_exec($ch);
curl_close($ch);

echo $curl_scraped_page;
?> 

İşte hataları giderilmiş çalışan bir sürüm.

$url = 'http://dynupdate.no-ip.com/ip.php';
$proxy = '127.0.0.1:8888';
//$proxyauth = 'user:password';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
//curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyauth);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$curl_scraped_page = curl_exec($ch);
curl_close($ch);

echo $curl_scraped_page;

Proxy'lerinizden herhangi birinin kullanıcı adı ve parola gerektirmesi durumunda CURLOPT_PROXYUSERPWD ekledim. Verilerin $curl_scraped_page değişkenine döndürülmesi için CURLOPT_RETURNTRANSFER değerini 1 olarak ayarladım.

Değişkenin döndürülmesini durduracak ikinci bir ekstra curl_exec($ch); öğesini kaldırdım. Proxy IP'nizi ve portunuzu tek bir ayarda birleştirdim.

Ayrıca varsayılan olduğu için CURLOPT_HTTPPROXYTUNNEL ve CURLOPT_CUSTOMREQUEST öğelerini de kaldırdım.

Başlıkların döndürülmesini istemiyorsanız, CURLOPT_HEADER ifadesini yorumlayın.

Proxy'yi devre dışı bırakmak için null olarak ayarlamanız yeterlidir.

curl_setopt($ch, CURLOPT_PROXY, null);

Herhangi bir sorunuz olursa sormaktan çekinmeyin, ben her gün cURL ile çalışıyorum.

Yorumlar (6)

CURL PROXY için gerekli olan çeşitli CURL seçeneklerinin kullanımını açıkladım.

$url = 'http://dynupdate.no-ip.com/ip.php';
$proxy = '127.0.0.1:8888';
$proxyauth = 'user:password';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);         // URL for CURL call
curl_setopt($ch, CURLOPT_PROXY, $proxy);     // PROXY details with port
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyauth);   // Use if proxy have username and password
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5); // If expected to call with specific PROXY type
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);  // If url has redirects then go to the final redirected URL.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);  // Do not outputting it out directly on screen.
curl_setopt($ch, CURLOPT_HEADER, 1);   // If you want Header information of response else make 0
$curl_scraped_page = curl_exec($ch);
curl_close($ch);

echo $curl_scraped_page;
Yorumlar (2)

İşte projelerim için kullandığım iyi test edilmiş bir fonksiyon ve detaylı kendi kendini açıklayan yorumlar


Sunucu güvenlik duvarı tarafından 80 dışındaki bağlantı noktalarının engellendiği birçok zaman vardır, bu nedenle kod localhost'ta iyi çalışıyor ancak sunucuda çalışmıyor gibi görünür

function get_page($url){

global $proxy;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
//curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_HEADER, 0); // return headers 0 no 1 yes
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return page 1:yes
curl_setopt($ch, CURLOPT_TIMEOUT, 200); // http request timeout 20 seconds
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Follow redirects, need this if the url changes
curl_setopt($ch, CURLOPT_MAXREDIRS, 2); //if http server gives redirection responce
curl_setopt($ch, CURLOPT_USERAGENT,
    "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7");
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies.txt"); // cookies storage / here the changes have been made
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookies.txt");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // false for https
curl_setopt($ch, CURLOPT_ENCODING, "gzip"); // the page encoding

$data = curl_exec($ch); // execute the http request
curl_close($ch); // close the connection
return $data;
}
Yorumlar (2)