1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

file_get_contents via proxy

Discussion in 'PHP' started by asgsoft, Apr 14, 2010.

  1. #1
    hey everyone!!

    Is it possible to use file_get_contents via a proxy?

    If so how?

    I've been looking online for a loooong time with no luck!

    Any help wouuld be greatly appreciated!

    thanks
     
    asgsoft, Apr 14, 2010 IP
  2. Kaimi

    Kaimi Peon

    Messages:
    60
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try
    
    <?php
    
    $opts = array('http' => array('proxy' => 'tcp://127.0.0.1:8080', 'request_fulluri' => true));
    $context = stream_context_create($opts);
    
    $data = file_get_contents('http://www.php.net', false, $context);
    
    echo $data;
    
    ?>
    
    PHP:
     
    Kaimi, Apr 14, 2010 IP
  3. asgsoft

    asgsoft Well-Known Member

    Messages:
    1,737
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    160
    #3
    I did come across that snippet.. but I don't actually understand it!!!
     
    asgsoft, Apr 15, 2010 IP
  4. ps3ubo

    ps3ubo Peon

    Messages:
    204
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Use CURL that lets you control nearly every header information sent.
     
    ps3ubo, Apr 15, 2010 IP
  5. WebWorth

    WebWorth Greenhorn

    Messages:
    89
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    20
    #5
    Totally agree cURL is the way to go here....
     
    WebWorth, Apr 15, 2010 IP
  6. asgsoft

    asgsoft Well-Known Member

    Messages:
    1,737
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    160
    #6
    But how would you pass it through a proxy?
     
    asgsoft, Apr 15, 2010 IP
  7. zappak

    zappak Active Member

    Messages:
    366
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    80
    #7
    try this...

    $ch = curl_init();
    curl_setopt ($ch, CURLOPT_PROXY, $proxy);
    curl_setopt($ch, CURLOPT_URL, url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    $a = curl_exec($ch);
    echo $a;
     
    zappak, Apr 16, 2010 IP
  8. asgsoft

    asgsoft Well-Known Member

    Messages:
    1,737
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    160
    #8
    The problem is, the script I already have and run uses file_get_contents.. so it would be much easier to adapt!
     
    asgsoft, Apr 16, 2010 IP
  9. trickyshaun

    trickyshaun Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    create a new function like
    
    function my_file_get_contents($url) {
            //Push your CURL Function here and return the content
    }
    
    Code (markup):
    and replace all file_get_contents( to my_file_get_contents(

    I think it will helpful :D . If you need full script , feel free when ask me.
     
    trickyshaun, Apr 17, 2010 IP
  10. ps3ubo

    ps3ubo Peon

    Messages:
    204
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    This is my own function I sometimes use.. just replace "file" in file_get_contents with "curl" to make curl_get_contents

    You need to get a valid working proxy ip:port or it will not show any content.
     
    ps3ubo, Apr 17, 2010 IP
  11. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #11
    @ps2ubo

    Aint your function missing a parem?

    function curl_get_contents($url)
     
    danx10, Apr 17, 2010 IP
  12. trickyshaun

    trickyshaun Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    yep :D he missing it :p
     
    trickyshaun, Apr 18, 2010 IP
  13. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #13
    First, read this comment on the PHP manual page. A lot of it is irrelevant, but the proxy stuff is there.
    http://www.php.net/manual/en/function.file-get-contents.php#58758

    Next, jump back to the top of the page and continue by paying special attention to the context argument of the file_geT_contents function.
    This is going to lead you to the stream_context_create manual page eventually http://www.php.net/manual/en/function.stream-context-create.php
    Which will in turn lead you to manual pages for all of the options you're going to need for the stream.

    When all is said and done, it's probably going to be easier, and more effective in the long run, to create a function that uses cURL and then using sed or something to replace all occuranges of file_get_contents in your script to your functions name. You're more than likely going to have to make replacements to add the context argument to all of your existing calls anyways.
     
    joebert, Apr 20, 2010 IP
  14. asgsoft

    asgsoft Well-Known Member

    Messages:
    1,737
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    160
    #14
    I see ok!!

    Well I'll give the curl function a try with the addition of the missing parameter and see how thins go!

    thanks :)
     
    asgsoft, Apr 21, 2010 IP
  15. ps3ubo

    ps3ubo Peon

    Messages:
    204
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    lol sorry yes i forgot it lol xD
     
    ps3ubo, Apr 23, 2010 IP
  16. luxon17

    luxon17 Peon

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #16
    You got this done yet? i'm looking for the same.
     
    luxon17, Apr 22, 2011 IP