cURL没有获取预期的POST响应 - 错误405

时间:2014-03-10 17:27:20

标签: php cakephp curl highcharts http-status-code-405

我需要从Highcharts导出服务器中获取多个图表(因为我还没有在我自己的服务器上实现该功能)。

Highcharts在这里展示了一个非常明确的例子: http://export.highcharts.com/demo

我尝试使用Linux中的curl命令成功模拟该表单。

但是,当尝试从PHP(特别是从CakePHP控制器)运行curl时,我收到HTTP错误:

  

HTTP状态405 - 不支持请求方法“POST”

出了点问题,因为POST方法可以从导航器和curl命令交互工作。

此外,我测试了PHP脚本,将其指向posttestserver.com,这是一个旨在调试POST请求结果的站点。我的测试结果如下:http://www.posttestserver.com/data/2014/03/10/10.18.221071980127

这是CakePHP控制器方法的代码:

public function obtenir() {

    $ch = curl_init("http://export.highcharts.com/demo");

    curl_setopt($ch, CURLOPT_POST, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, array(
            "content" => "options",
            "options" => "%7B%0D%0A%09xAxis%3A+%7B%0D%0A%09%09categories%3A+%5B%27Jan%27%2C+%27Feb%27%2C+%27Mar%27%2C+%27Apr%27%2C+%27May%27%2C+%27Jun%27%2C+%27Jul%27%2C+%27Aug%27%2C+%27Sep%27%2C+%27Oct%27%2C+%27Nov%27%2C+%27Dec%27%5D%0D%0A%09%7D%2C%0D%0A%09series%3A+%5B%7B%0D%0A%09%09data%3A+%5B29.9%2C+71.5%2C+106.4%2C+129.2%2C+144.0%2C+176.0%2C+135.6%2C+148.5%2C+216.4%2C+194.1%2C+95.6%2C+54.4%5D%0D%0A%09%7D%5D%0D%0A%7D%3B%0D%0A",
            "type" => "image%2Fpng",
            "width" => "",
            "scale" => "",
            "constr" => "Chart",
            "callback" => "function%28chart%29+%7B%0D%0A%09chart.renderer.arc%28200%2C+150%2C+100%2C+50%2C+-Math.PI%2C+0%29.attr%28%7B%0D%0A%09%09fill+%3A+%27%23FCFFC5%27%2C%0D%0A%09%09stroke+%3A+%27black%27%2C%0D%0A%09%09%27stroke-width%27+%3A+1%0D%0A%09%7D%29.add%28%29%3B%0D%0A%7D%0D%0A%09%09%09",
            ));
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: application/x-www-form-urlencoded; charset=UTF-8"));
    curl_setopt($ch, CURLINFO_HEADER_OUT, TRUE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

    $page = curl_exec($ch);

    $head = curl_getinfo($ch, CURLINFO_HEADER_OUT);
    debug($head);
    debug($page);

    curl_close($ch);

}

调试($ page)的结果是HTML格式的文本,其中包含重要内容:

HTTP状态405 - 不支持请求方法“POST”

我做错了什么?

非常感谢。

2 个答案:

答案 0 :(得分:4)

使用http_build_query()作为帖子参数数组的帖子。否则,它被识别为多部分表格发布。

curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query( array(...) ));

请确保POST网址也正确无误。

答案 1 :(得分:0)

Ur网址错误,请更改:

$ch = curl_init("http://export.highcharts.com/demo");

$ch = curl_init("http://export.highcharts.com");