简体   繁体   English

使用HTTP POST和PHP发送XML数据

[英]Sending XML data using HTTP POST with PHP

I need to send this XML 我需要发送这个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>

to a SMS gateway service. 到SMS网关服务。 The service listens for HTTP POST requests. 该服务侦听HTTP POST请求。 The XML must be embedded in the BODY of the POST request. XML必须嵌入POST请求的BODY中。

I'm using PHP and the CodeIgniter framework, but I'm a total PHP n00b, so ideally I'd need a thorough guide, but any pointers in the right direction would be appreciated. 我使用的是PHP和CodeIgniter框架,但是我总共使用的是PHP n00b,因此理想情况下,我需要一份详尽的指南,但是朝正确方向发展的任何指针将不胜感激。

you can use cURL library for posting data: http://www.php.net/curl 您可以使用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);

where postfield contains XML you need to send - you will need to name the postfield the API service (Clickatell I guess) expects postfield包含您需要发送的XML的位置-您将需要为API服务(我猜是Clickatell)期望的postfield命名

Another option would be file_get_contents() : 另一个选项是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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM