Save cookies for remote web pages

后端 未结 2 1765
迷失自我
迷失自我 2020-12-29 12:41

I\'ve got a PHP application which needs to grab the contents from another web page, and the web page I\'m reading needs a cookie.

I\'ve found info on how to make thi

相关标签:
2条回答
  • 2020-12-29 13:30

    You'd probably be better off using cURL. Use curl_setopt to set up the cookie handling options.

    If this is just a one-off thing, you could use Firefox with Live HTTP Headers to get the header, then paste it into your PHP code.

    0 讨论(0)
  • 2020-12-29 13:38

    Shazam - that worked ! Thx soooo much ! In case someone else stumbles upon this page, here's what was needed in detail:

    1. install cURL (for me it'was as simple as 'sudo apt-get install php5-curl' in ubuntu)
    2. change the prior-listed PHP to the following:

      <?php
      
      $cr = curl_init('http://remoteServer/login.php?user=xxx&pass=yyy');
      curl_setopt($cr, CURLOPT_RETURNTRANSFER, true); 
      curl_setopt($cr, CURLOPT_COOKIEJAR, 'cookie.txt');   
      $whoCares = curl_exec($cr); 
      curl_close($cr); 
      
      $cr = curl_init('http://remoteServer/pageicareabout.html');
      curl_setopt($cr, CURLOPT_RETURNTRANSFER, true); 
      curl_setopt($cr, CURLOPT_COOKIEFILE, 'cookie.txt'); 
      $documentiwant = curl_exec($cr);
      curl_close($cr);
      
      ?>
      

    Above code snippet heavily influenced by http://www.weberdev.com/get_example-4555.html.

    0 讨论(0)
提交回复
热议问题