0

I am having difficulty formatting the XML response from a php curl xml api for infobip. My code below loops sms to mobile numbers. But i want to format the XML responses to show the messages successfully sent and those that were not successful using a conditional if else statement.

while ($row = mysql_fetch_array($result))
{

    // XML-formatted data
    $xmlString='
            <SMS> <authentication> <username>'.$user.'</username> <password>'.$pass.'</password> </authentication> <message> <sender>'.$sender.'</sender> <text>'.$message.'</text> <recipients> <gsm>'.$mobileno.'</gsm>
        //<gsm>'.$mobileno1.'</gsm> </recipients> </message> </SMS>';

    $fields = "XML=" . urlencode($xmlString);

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $postUrl);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
    $response = curl_exec($ch);

    curl_close($ch);


if($response->status == '0') {
echo "message successfully delivered to ".$mobileno. "<br>" ;
}else{
echo "error sending message to ".$mobileno."<br>" ;
         }
   }

The problem i have is being able to set or parse the XML response for to get the <status> XML tags. Currently this how the XML responses outputs the results without formatting.

<?xml version="1.0" encoding="UTF-8"?> <results> <result><status>1</status><messageid></messageid><destination>23421</destination></result> </results><?xml version="1.0" encoding="UTF-8"?> <results> <result><status>0</status><messageid></messageid><destination>23412</destination></result> </results><?xml version="1.0" encoding="UTF-8"?> <results> <result><status>0</status><messageid></messageid><destination>23444</destination></result> </results>

I would appreciate to get this solved

3
Contributors
2
Replies
23
Views
1 Year
Discussion Span
Last Post by diafol
0

Neither what you are sending nor what you are getting are valid XMLs. Althowgh there is a probability that the request is meant to be XML like and not XML (maybe that is why you add XML= in front) the response should have been a valid XML. The question is , is what you are getting for one request or for three?

In the response you posted there are three different XMLs that have the same structure. If you got them all in one response you could split them and use SimpleXML parser. If it is the response of all requests and each one of them have each own XML than just use SimpleXML parser to take the status.

0

Just wondering - the infobip allows json response as well as XML. JSON can be converted to PHP array easily with json_decode(), making life a lot easier. Just a thought.

The request (same message to multiple recipients) could be in this format:

<?php
$request = new HttpRequest();
$request->setUrl('https://api.infobip.com/sms/1/text/single');
$request->setMethod(HTTP_METH_POST);

$request->setHeaders(array(
  'accept' => 'application/json',
  'content-type' => 'application/json',
  'authorization' => 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=='
));

$request->setBody('{  
   "from":"InfoSMS",
   "to":[  
      "41793026727",
      "41793026834"
   ],
   "text":"Test SMS."
}');

try {
  $response = $request->send();

  echo $response->getBody();
} catch (HttpException $ex) {
  echo $ex;
}

The above code as taken from https://dev.infobip.com/docs/send-single-sms

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.