SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    CURL connect to proxy server

    Hi Guys!

    I'm trying to connect to a proxy server using PHP CURL method. The proxy IP is a hidemyass.com. I get a blank page when I try using the following script, am I doing something wrong?

    PHP Code:
    $url "http://www.mydomain.com";
    $ch curl_init($url);
            
    curl_setopt($chCURLOPT_FOLLOWLOCATION1);
            
    curl_setopt($chCURLOPT_RETURNTRANSFER1);
            
    curl_setopt($chCURLOPT_TIMEOUT10);
            
    curl_setopt($chCURLOPT_COOKIE$cookies);
            
    curl_setopt($chCURLOPT_POST1);
            
    curl_setopt($chCURLOPT_POSTFIELDS"field1=value1&field2=value2");
            
    curl_setopt($chCURLOPT_REFERER'http://www.google.com');
            
    curl_setopt($chCURLOPT_PROXYTYPECURLPROXY_HTTP);
            
    curl_setopt($chCURLOPT_PROXY"109.73.65.130");
            
    curl_setopt($chCURLOPT_HTTPHEADER
                array(
                
    "Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
                
    "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3",
                
    "Accept-Language: en-US,en;q=0.8",
                
    "Cache-Control: max-age=0",
                
    "Connection: keep-alive",
                
    "Content-Type: application/x-www-form-urlencoded",
                
    "User-Agent:".$_SERVER['HTTP_USER_AGENT'],
                
    "Cookie: ".implode(" ",$new_cookies)));
                
            
    $response curl_exec($ch);
            
    print_r($response); exit; 

  2. #2
    SitePoint Addict kduv's Avatar
    Join Date
    May 2012
    Location
    Atlanta, GA
    Posts
    244
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    The first thing to do is add some error checking so you can report any errors cURL may report. After doing that and testing your script, I saw it was timing out. Since proxies can be slow, I increased the timeout with no luck. So I checked the proxylist on the site you specified and I didn't see that proxy IP listed.. so it seems you may have a bad proxy.

    You also didn't specify the port for the IP. Many, if not most proxies use ports other than 80. You also have to pay attention to whether it's an HTTP proxy or a SOCKS proxy. If it's HTTP, you don't have to worry about setting the cURL option as HTTP is the default.

    I also removed some of the unneeded options.

    Here is my working (as of this post) example:
    PHP Code:
    <?php

    $url 
    "http://www.sitepoint.com/forums/showthread.php?928518-CURL-connect-to-proxy-server";

    // Testing proxy speed
    $loadtime time();

    $ch curl_init($url);
    curl_setopt($chCURLOPT_RETURNTRANSFER1);
    curl_setopt($chCURLOPT_TIMEOUT20);
    curl_setopt($chCURLOPT_PROXY"8.21.6.225:80"); // Don't forget the port of the proxy... they're not all 80

    $response curl_exec($ch);

    // Testing proxy speed
    echo "cURL time: " . (time() - $loadtime) . "s<br />\n";

    if (
    $response === false) {
        echo 
    "cURL Error: "curl_error($ch);
    } else {
        echo 
    $response;
    }

    ?>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •