使用HTTP POST使用PHP发送XML数据

Fro*_*ode 21 php xml post codeigniter http

我需要发送这个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>
Run Code Online (Sandbox Code Playgroud)

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

我正在使用PHP和CodeIgniter框架,但我总是PHP n00b,所以理想情况下我需要一个完整的指南,但任何正确方向的指针都将受到赞赏.

dus*_*oft 32

您可以使用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);
Run Code Online (Sandbox Code Playgroud)

其中postfield包含您需要发送的XML - 您需要将API服务(我猜想的Clickatell)命名为postfield

  • 然后,请参阅此博客文章:http://netevil.org/blog/2006/nov/http-post-from-php-without-curl (4认同)

GZi*_*ipp 23

另一种选择是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);
Run Code Online (Sandbox Code Playgroud)