【问题标题】:Sending XML data using HTTP POST with PHP通过 PHP 使用 HTTP POST 发送 XML 数据
【发布时间】:2010-12-12 06:30:21
【问题描述】:

我需要发送这个 XML

      <?xml version="1.0" encoding="UTF-8"?>
<gate>
   <country>NO</country>
   <accessNumber>1900</accessNumber>
   <senderNumber>1900</senderNumber>
   <targetNumber>4792267523</targetNumber>
   <price>0</price>
   <sms>
      <content><![CDATA[This is a test æøå ÆØÅ]]></content>
   </sms>
</gate>

到 SMS 网关服务。该服务侦听 HTTP POST 请求。 XML 必须嵌入到 POST 请求的 BODY 中。

我正在使用 PHP 和 CodeIgniter 框架,但我完全是 PHP n00b,所以理想情况下,我需要一份详尽的指南,但任何正确方向的指针都将不胜感激。

【问题讨论】:

    标签: php xml http codeigniter post


    【解决方案1】:

    您可以使用 cURL 库来发布数据: http://www.php.net/curl

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_URL, "http://websiteURL");
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, "XML=".$xmlcontent."&password=".$password."&etc=etc");
    $content=curl_exec($ch);
    

    postfield 包含您需要发送的 XML - 您需要将 postfield 命名为 API 服务(我猜是 Clickatell)所期望的

    【讨论】:

    • 是的,我希望...我问过 IT 人是否可以安装 cURL,但他不可能在合理的时间内完成。
    • 还有强大且非常漂亮的 pecl_http 扩展和各种 PEAR HTTP_* 包(更易于您的 IT 人员安装)。
    • @dusoft - 感谢您提供指向 netevil.org 的链接
    【解决方案2】:

    另一个选项是file_get_contents():

    // $xml_str = your xml
    // $url = target url
    
    $post_data = array('xml' => $xml_str);
    $stream_options = array(
        'http' => array(
            'method'  => 'POST',
            'header'  => 'Content-type: application/x-www-form-urlencoded' . "\r\n",
            'content' =>  http_build_query($post_data)));
    
    $context  = stream_context_create($stream_options);
    $response = file_get_contents($url, null, $context);
    

    【讨论】:

    • 是的,PHP 4.3 中引入了流,但对大多数用户来说是隐藏的。
    • 我收到错误:{警告:file_get_contents(nwmls.com/Schemas/General/EverNetQueryXML.xsd) [function.file-get-contents]:打开流失败:HTTP 请求失败! HTTP/1.1 404 Not Found in /home/phretscl/public_html/xml/pulldata.php on line 42}
    • 等等问题不是“我如何发送 XML”而不是“我如何接收 XML 然后阅读它”?
    猜你喜欢
    • 2014-03-12
    • 1970-01-01
    • 2013-07-21
    • 2011-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-14
    相关资源
    最近更新 更多