1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

need a code to use HTTP Proxies for a script

Discussion in 'PHP & Perl' started by megamania, Apr 28, 2013.

  1. megamania

    megamania Newbie

    Joined:
    Nov 17, 2008
    Messages:
    20
    Likes Received:
    0
    i want to code a script which uses HTTP Proxies to hide the server ip is this possible to do if so how ?? some guidance please
     
  2. Wister_fr

    Wister_fr Registered Member

    Joined:
    Sep 6, 2012
    Messages:
    62
    Likes Received:
    23
    Location:
    Internet
    You just need to use cURL instead of file_get_content().

    Ex:

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

    Cf: stackoverflow . com/questions/5211887/how-to-use-curl-via-a-proxy
     
    • Thanks Thanks x 1
    Last edited: Apr 28, 2013
  3. johnmacklen

    johnmacklen Registered Member

    Joined:
    Apr 26, 2013
    Messages:
    51
    Likes Received:
    2
    hi..
    I send you some little steps to hide server ip... A common way to hid your ip address, is to use a proxy server to process your http requests. Setting up your browser to use a proxy server is easy enough, but setting up scripts and programs to use proxies can be a little harder.
    If the proxy that you are using requires authentication, you will simply add the proxy authorization http header, in a manner similar to what is shown in the last php tutorial. Logging in to a proxy is done by combining the username and password, only separated by a single colon ":", then encoding it using base64, and send it in the Proxy-Authorization http header.
    thank you
     
  4. CodingAndStuff

    CodingAndStuff Regular Member

    Joined:
    May 6, 2012
    Messages:
    239
    Likes Received:
    88
    Occupation:
    Swagstronaut
    Location:
    You can't have my bots. Sorry :'(
    What language? As stated above, you could use cURL and just set the options to use a proxy via CURLOPT_PROXY. If you decide to do it in Python, you should check out the "Requests" class (which can be found here: http://docs.python-requests.org/ ). It's really robust and handles nearly all HTTP requests (GET, POST, PUT, DELETE, etc), cookies, and of course proxies.

    Example:

    Code:
    import requests
    response = requests.get("http://site.com/",headers={"accept":"application/json"},proxies={"http":"http://127.0.0.1:8888"})
    if response.status_code == "200":
        print "Response is: " + response.text
    
    It also has some cool options for handling binary data, JSON responses, and all that nifty little stuff. Python is also always a great option for web automation since it has tons of threading libraries and runs on any operating system (plus you don't have to compile all those dependencies :p ).
     
  5. stfuplease

    stfuplease Newbie

    Joined:
    Jun 25, 2013
    Messages:
    19
    Likes Received:
    24

    Couple of things:
    1) There's no call to curl_close() after curl_exec() and there should be.
    2) If you're not going to sanitize $_POST[1], then why not just pass it directly?
    3) There's no need to split the proxy up and make 2 separate calls, CURLOPT_PROXY accepts IP:pORT format.


    The same code, but with the above corrections:
    Code:
    $sProxy = "127.0.0.1:8888";
    $ch = curl_init($_POST[1]);
    $c  = curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $c  = curl_setopt($ch, CURLOPT_HEADER, 1);
    $c  = curl_setopt($ch, CURLOPT_PROXY, $sProxy);
    $x  = curl_exec($ch);
    $c  = curl_close($ch);

    The above isn't commented just for sake of consistency. A more elegant solution would be:
    Code:
    function fetchURL($sURL, $aOpt=array()) {
    	/[b][/b]/ INITIALIZE CURL
    	$ch = curl_init($sURL);
    	/[b][/b]/ SET OPTIONS
    	$c  = curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    	$c  = curl_setopt($ch, CURLOPT_HEADER, 1);
    	/[b][/b]/ IF OPTIONS HAVE BEEN PASSED, SET THEM (AND OVERRIDE ORIGINALS ABOVE)
    	foreach ($aOpt as $sKey=>$sVal) { curl_setopt($ch, $sKey, $sVal); }
    	/[b][/b]/ EXECUTE REQUEST
    	$x  = curl_exec($ch);
    	/[b][/b]/ CLOSE HANDLE
    	$c  = curl_close($ch);
    	/[b][/b]/ RETURN RESPONSE
    	return($x);
    }
    ?>

    And then you'd call it like:
    Code:
    /[b][/b]/ OPTIONS FOR REQUEST
    $aOpt  = array(CURLOPT_PROXY=>'127.0.0.1:8888', CURLOPT_HEADER=>0);
    /[b][/b]/ TARGET URL
    $sURL  = "ht[b][/b]tp[:/[b][/b]/dynupdate[b][/b].[b][/b]no-ip[b][/b].[b][/b]co[b][/b]m[b][/b]/[b][/b]ip.php";
    /[b][/b]/ THE RESULT
    $sData = fetchURL($sURL, $aOpt);
    /[b][/b]/ DISPLAY IT
    print($sData);

    The last example being the best, because of the optional CURLOPT_X overrides that can be passed in $aOpt. In addition, it could be further improved by using a static handle ($ch) so that it re-uses the same handle each time instead of creating new ones. It should be noted that most of this response is intended for the OP, not an attempt to correct or insult you. My original goal was to point out the issues I noticed with that code, but instead it turned into simplifying the process as much as possible (which will hopefully benefit the OP).
     
    • Thanks Thanks x 2
  6. Xictus

    Xictus Regular Member

    Joined:
    May 16, 2011
    Messages:
    390
    Likes Received:
    186
    Awesome, thanks for the corrections!
     
    Last edited: Jun 30, 2013