php - 使用 PHP 将 XML 发布到 URL 并处理响应

标签 php xml api post

多年来,我见过许多使用 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/

相关文章:

php - 无法通过 HTTPS 访问 SOAP Web 服务

java - Android 拉伸(stretch) TableLayout XML 中的项目

ruby-on-rails - Rails XML 生成,如 Active Model Serializer

Android - 不透明度淡入淡出可绘制

ios - 同步 iOS 设备和索尼运动相机 HDR-AS200V。获取完整的有声电影

python - 'object' 在 python 中有哪些方法?

javascript - 在以下情况下如何从每个数组元素中删除\'?

php - 如何在 htaccess 中调用 $_GET 变量和解析 url 时回显文本?

php - WordPress 数据库查询不返回结果

javascript - 尝试使用 Indeed API 根据输入字段检索职位时出错