質問

別のWebページからコンテンツを取得する必要のあるPHPアプリケーションがあり、現在読んでいるWebページにはCookieが必要です。

Cookieを取得したら、この呼び出しを行う方法に関する情報を見つけました( http://groups.google.com/group/comp.lang.php/msg/4f618114ab15ae2a )、ただし、Cookieを生成する方法、またはCookieがどのように/どこにあるかはわかりません保存しました。

たとえば、wgetでこのWebページを読むには、次のようにします。

wget --quiet --save-cookies cookie.file --output-document=who.cares \ 
  http://remoteServer/login.php?user=xxx&pass=yyy

wget --quiet --load-cookies cookie.file --output-document=documentiwant.html \
  http://remoteServer/pageicareabout.html

...私の質問は、PHPで「--save-cookies」ビットをどのように実行して、フォローアップPHP stream_context_create / file_get_contentsブロックでCookieを使用できるようにするかです:

$opts = array(http'=> array(
  'method'=> "GET",
  'header'=>
    "Accept-language: en\r\n" .
    "Cookie: **NoClueAtAll**\r\n"
  )
);

$context = stream_context_create($opts);
$documentiwant = file_get_contents("http://remoteServer/pageicareabout.html",
  0, $context);
役に立ちましたか?

解決

cURL を使用した方がよいでしょう。 Cookie処理オプションを設定するには、 curl_setopt を使用します。

これが1回限りの場合、FirefoxでライブHTTPヘッダーを使用して、ヘッダーを作成し、PHPコードに貼り付けます。

他のヒント

Shazam -うまくいきました!本当にすごい!他の誰かがこのページを偶然見つけた場合、詳細が必要でした:

  1. cURLをインストールします(私にとっては 「sudo apt-get install」のように簡単 Ubuntuのphp5-curl ')
  2. 変更 事前にリストされているPHPを次のようにします。

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

上記のコードスニペットは http://www.weberdev.com/get_example-4555の影響を強く受けています。 html

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top