質問

私はURLの内容を取得するためにPHPの関数file_get_contents()を使用していますし、私は、変数$http_response_headerを通じてヘッダを処理します。

さて問題は、URLのいくつかは、いくつかのデータは、(例えば、ログインページ)URLにポストする必要があることです。

どのように私はそれを行うのですか?

私はそれを行うことができるかもしれstream_contextを使用して実現するが、私は完全には明らかではないと思います。

感謝します。

役に立ちましたか?

解決

file_get_contentsするを使用してHTTP POSTリクエストを送信すると、実際には、その難しいことではありません:あなたが推測として、あなたが持っています$contextパラメータを使用します。


PHPマニュアルに与えられた例は、このページでは、あります: HTTPコンテキストオプション の(引用)

$postdata = http_build_query(
    array(
        'var1' => 'some content',
        'var2' => 'doh'
    )
);

$opts = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => 'Content-Type: application/x-www-form-urlencoded',
        'content' => $postdata
    )
);

$context  = stream_context_create($opts);

$result = file_get_contents('http://example.com/submit.php', false, $context);

基本的に、あなたは右のオプションは、<全角>との(そのページの完全なリストがある)、ストリームを作成する必要があり、かつfile_get_contentsない第3のパラメータとしてそれを使用する - より多くの何も;-)


追記として:一般的にHTTPのPOSTリクエストを送信するために、言えば、我々はすべてのオプションの多くを提供し、カールを使用する傾向がある - しかし、ストリームは...誰もが知っているしないことを残念PHPの素敵なものの一つです。..ます。

他のヒント

代わり、あなたはまた、

の関数fopenのを使用することができます
$params = array('http' => array(
    'method' => 'POST',
    'content' => 'toto=1&tata=2'
));

$ctx = stream_context_create($params);
$fp = @fopen($sUrl, 'rb', false, $ctx);
if (!$fp)
{
    throw new Exception("Problem with $sUrl, $php_errormsg");
}

$response = @stream_get_contents($fp);
if ($response === false) 
{
    throw new Exception("Problem reading data from $sUrl, $php_errormsg");
}
$sUrl = 'http://www.linktopage.com/login/';
$params = array('http' => array(
    'method'  => 'POST',
    'content' => 'username=admin195&password=d123456789'
));

$ctx = stream_context_create($params);
$fp = @fopen($sUrl, 'rb', false, $ctx);
if(!$fp) {
    throw new Exception("Problem with $sUrl, $php_errormsg");
}

$response = @stream_get_contents($fp);
if($response === false) {
    throw new Exception("Problem reading data from $sUrl, $php_errormsg");
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top