我在PHP中执行POST请求,服务器在发布完成后发回一些文本。 这是代码:
<?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: closern".
"Content-Length: ".strlen($query)."rn",
'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:xampphtdocsdirectory Search_Result.php on line 49
string(269) " Nandimobile
19 Banana Street, American House East legon
IT Software products and services0302503313 0244709575 "
提前感谢
您需要为 POST 指定内容类型。
$contextData = array (
'method' => 'POST',
'header' => "Connection: closern".
"Content-Type: application/x-www-form-urlencodedrn".
"Content-Length: ".strlen($query)."rn",
'content'=> $query );
1,您没有收到您所说的错误,这只是通知(您可以在php.ini配置中禁用它)
2,您可以通过像这样设置内容类型标头来简单地避免此问题:
$contextData = array (
'http'=>array(
'method' => 'POST',
'header' => "".
"Connection: closern".
"Content-Length: ".strlen($query)."rn".
"Content-type: "."application/x-www-form-urlencoded"."rn",
"content"=> $query )
);
更多信息在这里 : http://php.net/manual/en/function.stream-context-create.php
在file_get_contents
之前添加@
并尝试
$result = @file_get_contents (
'http://infolinetest.nandiclient.com/search/searches/requestData.xml', // page url
false,
$context);