PHP關於file_get_contens函數向本地檔案傳參數的問題
源碼如下:
$str_query = "userId=admin&gameId=game1&serverId=server1";
$context= stream_context_create(array(
'http'=> array(
'method'=> 'POST',
'timeout' => 10,
'header'=> 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/534.13 (KHTML, like Gecko) Chrome/9.0.597.98 Safari/534.13\r\nContent-type: application/x-www-form-urlencoded\r\n',
'content'=> $str_query
)
));
echo file_get_contents("interface.php",true, $context);
?>
本地Aparche伺服器上就兩個檔案。一個是放以上源碼的檔案。一個是interface.php檔案。
我在此檔案上寫上如上的源碼。用file_get_contents向interface.php檔案POST資料。結果interface.php沒有接收到資料。
不知道是在哪裡出錯了。求高手賜教!!!!
回複討論(解決方案)
需要有協議類型
echo file_get_contents(" http://localhost/interface.php",true, $context);
另外參數數組中
header 項要寫成
'header'=> "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/534.13 (KHTML, like Gecko) Chrome/9.0.597.98 Safari/534.13\r\nContent-type: application/x-www-form-urlencoded\r\n",
只在雙引號中 \r\n 才會生效
另外參數數組中
header 項要寫成
'header'=> "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/534.13 (KHTML, like Gecko) Chrome/9.0.597.98 Safari/534.13\r\nContent-type: application/x-www-……
哦? 就是說沒有HTTP這樣就肯定不行了咯!!!
這點參數直接curl get 就是了
引用 3 樓 的回複:
另外參數數組中
header 項要寫成
'header'=> "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/534.13 (KHTML, like Gecko) Chrome/9.0.597.98 Safari/534.13\r\nContent-type: app……
沒有互連網協議,只不過是檔案系統函數。你非要猴子去飛,那是辦不到的。孫悟空也只是藉著菩提老祖的口訣翻翻筋鬥而已
為什麼會file_get_contents能擷取到完整的json內容,而用curl擷取卻是不完整的json內容呢?