curl through proxy returns no content

前端 未结 3 1305
难免孤独
难免孤独 2021-01-03 03:09

I\'m working on a PHP script right now which sends requests to our school\'s servers to get real-time information about class sizes for different courses. The script runs pe

相关标签:
3条回答
  • 2021-01-03 03:31

    You can set CURLOPT_STDERR and CURLOPT_VERBOSE curl options to save your errors in a file. Also, you may use curl_error() function. BTW, by default, curl should show all errors in STDERR.

    Besides, for general check, you can simply specify selected proxy in you browser configuration properties, try to open specific service in browser and see, whether correct response is returned.

    UPDATE:

    CURLOPT_HTTPPROXYTUNNEL is used to make curl call CONNECT HTTP method when requesting proxy server (see here for details). I tested code without this option - it worked successfully.

    Code I used:

    $proxy = "75.147.173.215:8080";
    $proxy = explode(':', $proxy);
    $url = "http://google.com";
    
    $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;
    
    0 讨论(0)
  • 2021-01-03 03:38

    Here is a well tested function which i used for my projects with detailed self explanatory comments


    There are many times when the ports other than 80 are blocked by server firewall so the code appears to be working fine on localhost but not on the server , try using port 80 proxies

    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;
    }
    
    0 讨论(0)
  • 2021-01-03 03:40

    I use the following code if I have to use a proxy with curl:

    $proxy = "127.0.0.1:8080"; // or something like that
    
    if($proxy !== null){
    
        // no need to specify PROXYPORT again
        curl_setopt($ch, CURLOPT_PROXY, $proxy);
    
        // to make the request go through as though proxy didn't exist
        curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 0);
    
    }
    
    0 讨论(0)
提交回复
热议问题