file_get_contents proxy

Discussion in 'PHP' started by Tectonicz, Aug 18, 2009.

  1. #1
    How would I use file_get_contents via a proxy?
     
    Last edited: Aug 18, 2009
    Tectonicz, Aug 18, 2009 IP
  2. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Well ofcourse it doesn't work, you have set the proxy for CURL. This has no effect on file_get_contents.

    Though, if you have curl, why don't you just use it? I don't think file_get_contents has proxy support.
     
    premiumscripts, Aug 18, 2009 IP
  3. Tectonicz

    Tectonicz Peon

    Messages:
    88
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hmm, can you give me an example?, I dont like using curl when fetching contents. (i find it confusing)
     
    Tectonicz, Aug 18, 2009 IP
  4. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Actually, you can in PHP 5.1.0+:

    $opts = array(
      'http'=>array(
        'method'=>"GET",
        'header'=>"Accept-language: en\r\n" .
                  "Cookie: foo=bar",
        'proxy'=>"tcp://10.54.1.39:8000"
      )
    );
    
    
    $context = stream_context_create($opts);
    
    $file = file_get_contents('http://www.example.com/', false, $context);
    
    PHP:
     
    premiumscripts, Aug 18, 2009 IP
  5. Tectonicz

    Tectonicz Peon

    Messages:
    88
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Im trying to submit and retrieve a file form an external site. Im using file_get_contents , because Im unfamilar of how to do so with cURL.

    The external site has a form which you upload a file and it echos some content on the same page.

    This is the html source from the external site:

    <form enctype="multipart/form-data" method="post" >
    	<p><input type="hidden" name="MAX_FILE_SIZE" value="2097152" />
    	<input name="upload" type="file" />
    	<input type="submit" value="Show" /></p>
    </form>
    Code (markup):
    This is the bit where the content is echo'd after the upload:
    <div id="output">
    
    the echo'd content from the uploaded file is contained within this tag 
    on the external site
    </div>
    Code (markup):
    I managed to grab the form but it dont function, I click a file to upload, but I cant get the file to display on my site.

    Heres the code:

    <?php 
        $url = "http://www.externalsite.com";
        $rurl = file_get_contents($url);
    
               $i = 1; 
               while($i <= 5)
              {
                    $start = explode('<form enctype="multipart/form-data" method="post" >', $rurl);
                    $end = explode('</form>', $start[$i]);
                    $table .= $end[0];
                    $i++;
               }            
               echo $table;
                    
    ?>
    Code (markup):
     
    Tectonicz, Aug 30, 2009 IP
  6. osmasters

    osmasters Well-Known Member

    Messages:
    453
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    100
    #6
    Here, what is need to have $i loop?
     
    osmasters, Aug 30, 2009 IP
  7. picos

    picos Active Member

    Messages:
    155
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    81
    #7
    yes, you dont need $i.
    I think, problem here
    $url="http://www.externalsite.com/index.html"; -> full link.
     
    picos, Aug 31, 2009 IP
  8. Tectonicz

    Tectonicz Peon

    Messages:
    88
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Thanks but I don't think you understand my question, I was suggested to use curl, i tried using curl and got stuck because curl requires you to define the form name or form action which the site does not have, so i tried it with file_get_contents, I managed to grab the form but its not echoing the output after i click the submit button, all my code does so far is embed the upload form.

    On the external site I'd upload a file and it would echo the ouput on the same page just below the form within the: <div id="output"></div> tags.
     
    Tectonicz, Aug 31, 2009 IP
  9. ps3ubo

    ps3ubo Peon

    Messages:
    204
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    file_get_content only GETs the content not POSTs, use curl or file_post_contents that is a function that someone made that make using curl easier (found searching google).
     
    ps3ubo, Aug 31, 2009 IP
  10. OverDark

    OverDark Banned

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    if you post the contents and you want to get the <div> then you can run a RegEx (ereg,preg_match) .. but if you need to POST and after that to grab the echoed <div> (and the rest of the code) then you should use cURL
     
    OverDark, Aug 31, 2009 IP
  11. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #11
    Just another note, every form must have an action page. If left empty, usually it is the same page as where the form is.
     
    ThePHPMaster, Aug 31, 2009 IP