多年来,我见过许多使用 PHP 发布数据的方法,但我很好奇建议的方法是什么,假设有一种方法。或者也许有一种不言而喻但半普遍接受的方法来做到这一点。这还包括处理响应。
最佳答案
虽然 Snoopy 脚本可能很酷,但如果您希望仅使用 PHP 发布 xml 数据,为什么不使用 cURL?它很简单,具有错误处理功能,并且是您随身携带的有用工具。下面是如何在 PHP 中使用 cURL 将 XML 发布到 URL 的示例。
// url you're posting to
$url = "http://mycoolapi.com/service/";
// your data (post string)
$post_data = "first_var=1&second_var=2&third_var=3";
// create your curl handler
$ch = curl_init($url);
// set your options
curl_setopt($ch, CURLOPT_MUTE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); //ssl stuff
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// your return response
$output = curl_exec($ch);
// close the curl handler
curl_close($ch);
关于php - 使用 PHP 将 XML 发布到 URL 并处理响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/471506/