由于回答而更新:
谁能指出以下两者的区别:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api_root);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, "xml"); // tried http_build_query also
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Added this, still no good
return curl_exec($ch); // returns false
和:
$curl = "curl -X POST -d 'xml' {$api_root}";
return `$curl`; // returns expected xml from server
和/或
更一般地说,在 php 的 libcurl 默认值/ header 和命令行上的 curl 默认值/ header 之间是否有任何好的转换/引用?
我知道这几乎是 curl CLI to curl PHP 的骗局和 CLI CURL -> PHP CURL但我希望有更明确的东西。
最佳答案
当您使用反引号时,PHP 会调用一个 shell。这可能很危险,尤其是当您在命令中包含变量时。如果有人有办法影响 $api_root
的值,他们将能够在您的系统上调用任何命令。
使用 API 更安全,而且可能更快,而且 curl 库已加载到 PHP 中。
至于为什么它不起作用,似乎其他人已经回答了这个问题:)
关于php curl vs cli curl,发布 xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6794916/