cURL未提取预期的POST响应 - 错误405 [英] cURL not fetching the expected POST response - error 405

查看:1278
本文介绍了cURL未提取预期的POST响应 - 错误405的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to fetch several charts from the Highcharts export server (since I haven't implemented that feature on my own server yet).

Highcharts显示非常清晰示例:
http://export.highcharts.com/demo

Highcharts shows a very clear example here: http://export.highcharts.com/demo

我试图使用来自Linux的curl命令模拟该表单。

I tried to simulate that form using the curl command from Linux, successfully.

但是,当尝试从PHP具体来自CakePHP控制器),我收到一个HTTP错误:

But, when trying to run curl from PHP (specifically from a CakePHP controller), I get a HTTP error:


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

HTTP Status 405 - Request method 'POST' not supported

有些事情是错误的,因为POST方法从导航器和curl命令交互式地工作。

Something is wrong, since the POST method worked interactivelly from the navigator and from curl command.

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

Also, I tested the PHP script pointing it to posttestserver.com, a site intended to debugging the result of POST requests. The result of my test is here: http://www.posttestserver.com/data/2014/03/10/10.18.221071980127

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

This is the code of the method of the CakePHP controller:

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);

}

调试结果包含重要内容的文本:

The result of debug($page) is an HTML formatted text with the significant content:

非常感谢。

推荐答案

code> http_build_query()用于post参数数组。

Use http_build_query() for the post over your post parameter array. Otherwise its recognized as multipart form posting.

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

请确认 POST 正确。

这篇关于cURL未提取预期的POST响应 - 错误405的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆