おせわになってます。
現在、file_get_contentsでのPOST送信がしたいのですが
何故か、下記のエラーが出るので困ってます。
ご存知の方、おられましたらご教示下さい。
エラー名:
PHP Warning: file_get_contents(http://localhost:8000/reception.php):
failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found
URLが間違っているとは思えないのですが?
◎期待する答え
Array([key1] => value1 [key2] => value2)
◎下記URLでアクセス
http://localhost:8000/send.php
>php -S localhost:8000 -t nitta
◎send.php(nitp)ta/send.php
<?php // 送信データ $data = array( "key1" => "value1", "key2" => "value2" ); // URLエンコードされたクエリ文字列を生成 $data = http_build_query($data, "", "&"); // ストリームコンテキストのオプションを作成 $options = array( // HTTPコンテキストオプションをセット 'http' => array( 'method'=> 'POST', 'header'=> 'Content-Type: application/x-www-form-urlencoded', 'content' => $data ) ); // ストリームコンテキストの作成 $context = stream_context_create($options); // POST送信 $contents = file_get_contents('http://localhost:8000/reception.php', false, $context); // reception.php のレスポンスを表示 echo $contents;
reception.php(/reception.php)⇒nittaフォルダと同じ階層
<?php print_r($_POST);
訂正版
◎send.php(nitta/send.php)
<?php $url = 'http://localhost:8000/api/reception.php/'; // POSTデータ $data = array( "key1" => "value1", "key2" => "value2" ); $data = http_build_query($data, "", "&"); $ch = curl_init(); // はじめ curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch,CURLOPT_POST, true); //データの配列を設定する curl_setopt($ch, CURLOPT_POSTFIELDS, $data); $html = curl_exec($ch);//実行 curl_close($ch); var_dump($html); ?>
⇒出力結果
bool(false)