Wie benutzt man CURL über einen Proxy?

Ich möchte curl so einstellen, dass es einen Proxy-Server verwendet. Die URL wird von einem HTML-Formular bereitgestellt, was kein Problem darstellt. Ohne den Proxy funktioniert es gut. Ich habe auf dieser und anderen Seiten Code gefunden, aber sie funktionieren nicht. Für jede Hilfe bei der Suche nach der richtigen Lösung wäre ich sehr dankbar. Ich habe das Gefühl, dass die unten aufgeführten Lösungen nahe dran sind, aber dass ich etwas übersehe. Ich danke Ihnen.

Den folgenden Code habe ich von http://www.webmasterworld.com/forum88/10572.htm übernommen, aber er gibt eine Fehlermeldung über eine fehlende T_VARIABLE in Zeile 12 aus.

<?

$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);
?>

Der nachfolgende Code stammt von https://stackoverflow.com/questions/4802816/curl-through-proxy-returns-no-content

<?

$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;
?>

ist derzeit live auf pelican-cement.com, funktioniert aber auch nicht.

UPDATE: Vielen Dank für Ihre Hilfe, ich habe die oben genannten Änderungen vorgenommen. Jetzt wird nur noch ein leerer Bildschirm angezeigt.

<?

$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;
?> 

Hier ist eine funktionierende Version, in der die Fehler beseitigt wurden.

$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;

Ich habe CURLOPT_PROXYUSERPWD hinzugefügt, falls einer Ihrer Proxies einen Benutzernamen und ein Passwort benötigt. Ich habe CURLOPT_RETURNTRANSFER auf 1 gesetzt, so dass die Daten an die Variable $curl_scraped_page zurückgegeben werden.

Ich habe ein zweites zusätzliches curl_exec($ch); entfernt, das die Rückgabe der Variablen verhindern würde. Ich habe die IP und den Port des Proxys in einer Einstellung zusammengefasst.

Ich habe auch CURLOPT_HTTPPROXYTUNNEL und CURLOPT_CUSTOMREQUEST entfernt, da dies der Standard war.

Wenn Sie die Header nicht zurückgeben wollen, kommentieren Sie CURLOPT_HEADER aus.

Um den Proxy zu deaktivieren, setzen Sie ihn einfach auf null.

curl_setopt($ch, CURLOPT_PROXY, null);

Wenn Sie Fragen haben, können Sie diese gerne stellen, ich arbeite jeden Tag mit cURL.

Kommentare (6)

Ich habe die Verwendung der verschiedenen CURL-Optionen erklärt, die für CURL PROXY erforderlich sind.

$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;
Kommentare (2)

Hier ist eine gut getestete Funktion, die ich für meine Projekte verwendet habe, mit ausführlichen selbsterklärenden Kommentaren


Es kommt häufig vor, dass andere Ports als 80 von der Firewall des Servers blockiert werden, so dass der Code auf dem localhost gut zu funktionieren scheint, aber nicht auf dem Server

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;
}
Kommentare (2)