问题
I need to fetch several charts from the Highcharts export server (since I haven't implemented that feature on my own server yet).
Highcharts shows a very clear example here: http://export.highcharts.com/demo
I tried to simulate that form using the curl command from Linux, successfully.
But, when trying to run curl from PHP (specifically from a CakePHP controller), I get a HTTP error:
HTTP Status 405 - Request method 'POST' not supported
Something is wrong, since the POST method worked interactivelly from the navigator and from curl command.
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
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:
HTTP Status 405 - Request method 'POST' not supported
What am I doing wrong?
Thank you very much.
回答1:
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(...) ));
Please make sure the POST
url is also correct.
回答2:
Ur URL is wrong, change:
$ch = curl_init("http://export.highcharts.com/demo");
to
$ch = curl_init("http://export.highcharts.com");
来源:https://stackoverflow.com/questions/22307079/curl-not-fetching-the-expected-post-response-error-405