cUrl - how to use proxy?

Discussion in 'PHP' started by chevchelios, Dec 19, 2011.

  1. #1
    I have the following code:
    <?
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'http://www.google.com/');
    $data = curl_exec($ch) or die("Curl exe failed");
    echo $data;
    curl_close($ch);
    ?>
    Code (markup):
    It works fine, it returns the google homepage, but if I try and add this line:
    curl_setopt($ch, CURLOPT_PROXY, '103.10.110.37:8080');
    Code (markup):
    IT DOESN'T WORK. Why? Can someone pleeaase help me ? I spend my last 2 hours trying to figure out what the h... is going on.
     
    chevchelios, Dec 19, 2011 IP
  2. hip_hop_x

    hip_hop_x Active Member

    Messages:
    522
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    90
    #2
    Add curl_timeout, maybe, curl can't connect to that ip.. Make sure it's not blocked by firewall.
     
    hip_hop_x, Dec 19, 2011 IP
  3. chevchelios

    chevchelios Active Member

    Messages:
    116
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #3
    @Thanks, I'll try what you said.

    Anyone any suggestions? Really, no one knows how to do this?
     
    chevchelios, Dec 21, 2011 IP
  4. Warfield

    Warfield Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I have found a manual, with this example you can download content but you can use this how you need.

    Function:

    <?php
    function getPage($proxy, $url, $referer, $agent, $header, $timeout) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, $header);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_PROXY, $proxy);
    curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    curl_setopt($ch, CURLOPT_REFERER, $referer);
    curl_setopt($ch, CURLOPT_USERAGENT, $agent);

    $result['EXE'] = curl_exec($ch);
    $result['INF'] = curl_getinfo($ch);
    $result['ERR'] = curl_error($ch);

    curl_close($ch);

    return $result;
    }
    ?>


    What is each thing?

    PHP cURL functions used

    • curl_init - initializes a cURL session.
    • curl_setopt - sets and option for a cURL transfer.
    • curl_exec - performs a cURL session.
    • curl_getinfo - gets information about the last transfer.
    • curl_error - returns a string containing the last error for the current session.
    • curl_close - close a cURL session.
    curl_setopt options used
    • CURLOPT_URL - the URL to scrap.
    • CURLOPT_HEADER - inlude/exclude the header?
    • CURLOPT_RETURNTRANSFER - return the transfer as a string or output it out directly? Use 1, i.e. return.
    • CURLOPT_PROXY - the HTTP proxy to tunnel request through.
    • CURLOPT_HTTPPROXYTUNNEL - tunnel through a given HTTP proxy? Use 1, i.e. tunnel.
    • CURLOPT_CONNECTTIMEOUT - it's obvious.
    • CURLOPT_REFERER - header to be used in a HTTP request.
    • CURLOPT_USERAGENT - "User Agent:" to be used in a HTTP request.

    Now, how to call this function?

    <?php
    $result = getPage(
    '[proxy IP]:[port]', // use valid proxy
    'http://www.google.com/search?q=twitter',
    'http://www.google.com/',
    'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.8) Gecko/2009032609 Firefox/3.0.8',
    1,
    5);

    if (empty($result['ERR'])) {
    // Job's done! Parse, save, etc.
    // ...
    } else {
    // Captcha or network problems?
    // ...
    }
    ?>

    It's easy, i have tested that and works perfect.
    PD: If proxy not works will show you a error, try looking for others proxys and test.
     
    Warfield, Dec 21, 2011 IP
  5. thegr4

    thegr4 Well-Known Member

    Messages:
    695
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    133
    #5
    thegr4, Dec 23, 2011 IP
  6. uspro

    uspro Peon

    Messages:
    25
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Warfield response looks good, try it and tell us if it works
     
    uspro, Jan 6, 2012 IP