我在php中发布帖子请求,服务器在帖子完成后发回一些文本。这是代码:
<?php // Create map with request parameters
$params = array ('username' => 'loginapi', 'password' => 'myapilogin', 'term'=> 'tema' );
// Build Http query using params
$query = http_build_query ($params);
// Create Http context details
$contextData = array (
'method' => 'POST',
'header' => "Connection: close\r\n".
"Content-Length: ".strlen($query)."\r\n",
'content'=> $query );
// Create context resource for our request
$context = stream_context_create (array ( 'http' => $contextData ));
// Read page rendered as result of your POST request
$result = file_get_contents (
'http://infolinetest.nandiclient.com/search/searches/requestData.xml', // page url
false,
$context);
// Server response is now stored in $result variable so you can process it
var_dump($result);
?>
问题是,即使我想要的结果如下,也会发生以下错误:
Notice: file_get_contents() [function.file-get-contents]: Content-type not specified
assuming application/x-www-form-urlencoded in C:\xampp\htdocs\directory \Search_Result.php on line 49
string(269) " Nandimobile
19 Banana Street, American House East legon
IT Software products and services0302503313 0244709575 "
提前完成
答案 0 :(得分:4)
您需要为POST指定内容类型。
$contextData = array (
'method' => 'POST',
'header' => "Connection: close\r\n".
"Content-Type: application/x-www-form-urlencoded\r\n".
"Content-Length: ".strlen($query)."\r\n",
'content'=> $query );
答案 1 :(得分:1)
1,你没有收到错误,就像你说的那样,它只是注意到(你可以在php.ini配置中禁用它)
2,您可以通过设置内容类型标题来避免此问题:
$contextData = array (
'http'=>array(
'method' => 'POST',
'header' => "".
"Connection: close\r\n".
"Content-Length: ".strlen($query)."\r\n".
"Content-type: "."application/x-www-form-urlencoded"."\r\n",
"content"=> $query )
);
此处有更多信息:http://php.net/manual/en/function.stream-context-create.php
答案 2 :(得分:0)
在@
之前添加file_get_contents
并尝试
$result = @file_get_contents (
'http://infolinetest.nandiclient.com/search/searches/requestData.xml', // page url
false,
$context);