Sending XML data using HTTP POST with PHP

102,201

Solution 1

you can use cURL library for posting data: 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

Solution 2

Another option would be 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);
Share:
102,201
Frode
Author by

Frode

Updated on July 22, 2022

Comments

  • Frode
    Frode almost 2 years

    I need to send this 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. The service listens for HTTP POST requests. The XML must be embedded in the BODY of the POST request.

    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.