3
votes

I'm making a script with PHP 7.1 to send SMS with the Huawei E3531 usb modem. There is no documenation and I have a lot of difficulties to find the solution. This is the script to send the sms but I get an error 100002. First, I recuperate the token and the sessionid with curl and then I use curl again to post the sms

<?php
//send Sms

$curl = curl_init();
$url = "http://192.168.8.1/api/webserver/SesTokInfo";
curl_setopt($curl, CURLOPT_URL, $url); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$content = curl_exec($curl);  
$xml = new simpleXMLElement($content);

$sess_id = $xml->SesInfo;
// echo $sess_id;
$tokInfo= $xml->TokInfo;
curl_close($curl);

$curl2 = curl_init('http://192.168.8.1/api/send-sms/');

$headers = array(
"X-Requested-With: XMLHttpRequest",
'Cookie:'. $sess_id,
'__RequestVerificationToken:'. $tokInfo,
'"Content-Type:text/xml"',
);
$data ="<request><Index>-1</Index><Phones><Phone>7777777</Phone></Phones><Sca/><Content>hello</Content><Length></Length><Reserved>1</Reserved><Date></Date></request" ; 
//print_r($headers);

curl_setopt($curl2, CURLOPT_HTTPHEADER, $headers);

curl_setopt($curl2, CURLOPT_POST, true);

curl_setopt($curl2, CURLOPT_POSTFIELDS, $data);

curl_setopt($curl2, CURLOPT_RETURNTRANSFER, true);

echo $content = curl_exec($curl2);

curl_close($curl2);

However I've made another script to get the sms already sent and it does work. Here is the code:

<?php
//get session_id and token
$curl = curl_init();
$url = "http://192.168.8.1/api/webserver/SesTokInfo";
curl_setopt($curl, CURLOPT_URL, $url); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$content = curl_exec($curl);  
$xml = new simpleXMLElement($content);

$sess_id = $xml->SesInfo;
// echo $sess_id;
$tokInfo= $xml->TokInfo;
curl_close($curl);

//set boxtype to 2 for retrieve sent sms
$data="<request><PageIndex>1</PageIndex><ReadCount>10</ReadCount><BoxType>2</BoxType><SortType>0</SortType><Ascending>0</Ascending><UnreadPreferred>1</UnreadPreferred></request>";

$curl2 = curl_init('http://192.168.8.1/api/sms/sms-list/');// A compléter

$headers = array(
'Cookie:'. $sess_id,
'__RequestVerificationToken:'. $tokInfo,
'Content-Type: application/x-www-form-urlencoded; charset=UTF-8',
);

//fetch sent sms
curl_setopt($curl2, CURLOPT_HTTPHEADER, $headers);

curl_setopt($curl2, CURLOPT_POST, true);

curl_setopt($curl2, CURLOPT_POSTFIELDS,$data);

curl_setopt($curl2, CURLOPT_RETURNTRANSFER, true);

$content = curl_exec($curl2);
echo $content;
curl_close($curl2);
$xml = new simpleXMLElement($content);

The 3 three steps used on this link (Sending and receiving SMS by command line with Huawei E3131 and HiLink on a debian system) to post sms on command line with the e3131 model (mine is e3531) worked for me but this is with command line. I can't answer on this thread because I'm new on this forum.

How can I fix this code?

2
I can't answer on this thread because I'm new on this forum. So you want to post an answer?Vinay

2 Answers

4
votes

you shoul add the date in the data request, and well closed it, this code is working for me:

 <?php
$curl = curl_init();
$url = "http://192.168.8.1/api/webserver/SesTokInfo";
curl_setopt($curl, CURLOPT_URL, $url); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$content = curl_exec($curl);  
$xml = new simpleXMLElement($content);

$sess_id = $xml->SesInfo;echo $sess_id;

// echo $sess_id;
$tokInfo= $xml->TokInfo;
curl_close($curl);
$curl2 = curl_init('http://192.168.8.1/api/sms/send-sms');

$headers = array(
"X-Requested-With: XMLHttpRequest",
'Cookie:'. $sess_id,
'__RequestVerificationToken:'. $tokInfo,
'"Content-Type:text/xml"',
);
$dateTime = date("Y-m-d H:i:s");
$data ="<request><Index>-1</Index><Phones><Phone>2135555555</Phone></Phones><Sca/><Content>salam</Content><Length>5</Length><Reserved>1</Reserved><Date>".$dateTime."</Date></request>" ; 
//print_r($headers);

curl_setopt($curl2, CURLOPT_HTTPHEADER, $headers);

curl_setopt($curl2, CURLOPT_POST, true);

curl_setopt($curl2, CURLOPT_POSTFIELDS, $data);

curl_setopt($curl2, CURLOPT_RETURNTRANSFER, true);

echo $content = curl_exec($curl2);

curl_close($curl2);

?>
2
votes

The way you get the token for the referenced device does not work for the reasons:
1 - Unapproved URL (not applicable to this device) to get the token and send the sms;
2 - soon, the $xml->SesInfo method called will not work;

However, I recommend that you take the token with the file_get_contents function as shown in the script below:

// Note the small changes to your script
// Take the token: 

$infoToken =  file_get_contents('http://192.168.8.1/api/webserver/token');
$xml = new simpleXMLElement($infoToken)
$token = $xml->token;

$content ="";
$lengContent = strlen($content);
$dateTime = date("Y-m-d H:i:s");
$headers = array(
    "__RequestVerificationToken: $token",
    "X-Requested-With: XMLHttpRequest",
    "Content-Type: application/x-www-form-urlencoded; charset=UTF-8"
);

$data ='<?xml version="1.0" encoding="UTF-8"?><request><Index>-1</Index><Phones><Phone>777777</Phone></Phones><Sca></Sca><Content>'.$content.'</Content><Length>'.$lengContent.'</Length><Reserved>1</Reserved><Date>'.$dateTime.'</Date></request>' ;

//connect with cURL
//http://192.168.8.1/api/send-sms : Unapprove URL;
//http://192.168.8.1/api/sms/send-sms : Approve URL;
$curl = curl_init('http://192.168.8.1/api/sms/send-sms');
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 300);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);

$content = curl_exec($curl);
var_dump($content);
curl_close($curl);

// Problem solved