PHP關於file_get_contens函數向本地檔案傳參數的問題

來源:互聯網
上載者:User
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內容呢?

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.