-1

我在 php 中做一个 post 请求,当 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: 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 " 

提前感谢

4

3 回答 3

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 );
于 2012-06-20T09:36:52.747 回答
1

1,你没有收到你说的错误,只是通知(你可以在php.ini配置中禁用它)

2,您可以通过像这样设置 content-type 标头来简单地避免这个问题:

$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

于 2012-06-20T09:37:34.657 回答
0

@在之前添加file_get_contents并尝试

$result =  @file_get_contents (
              'http://infolinetest.nandiclient.com/search/searches/requestData.xml',  // page url
              false,
              $context);
于 2012-06-20T09:35:10.780 回答