Post 请求 file_get_contents 数组

Post request with file_get_contents with arrays

我正在尝试 post 通过 file_get_contents 发出请求,但我收到 错误 500,我'我正在这样做:

   $params = array("pid" => "000209", "main_widget_pid" => "000209");

    $array = array("method" => "bottomline","params" => $params);

    $url = 'http://example.com/test';
    $data = array('app_key' => '8948a6aa12a1a23Yzglj17QO91Geg', 'methods' => $array);

    $options = array(
        'http' => array(
            'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
            'method'  => 'POST',
            'content' => http_build_query($data),
        ),
    );

$context  = stream_context_create($options); 
$result = file_get_contents($url, false, $context);
var_dump($result);

我认为问题出在数组的结构上。但是我看不出问题。

谁能给我一个方法?

试试这个补丁:

$data = array('app_key' => '8948a6aa12a1a23Yzglj17QO91Geg', 'methods' => $array);
$http_data = http_build_query($data);

$options = array(
    'http' => array(
        'header'  => "Content-type: application/x-www-form-urlencoded\r\n"
              . "Content-Length: " . strlen($data) . "\r\n",
        'method'  => 'POST',
        'content' => $http_data,
    ),
);

Content-length header 我猜很重要。

另外 $options 看起来像:

array (
  'http' => 
  array (
    'header' => 'Content-type: application/x-www-form-urlencoded
',
    'method' => 'POST',
    'content' => 'app_key=8948a6aa12a1a23Yzglj17QO91Geg&methods[method]=bottomline&methods[params][pid]=000209&methods[params][main_widget_pid]=000209',
  ),
)

确保它应该是这样的。

php 中的错误 500 表示您的脚本在传递此 $options 后出现致命异常。也许你应该调试那里出了什么问题...