我正在用PHP 7.1创建一个脚本,用华为E3531 usb调制解调器发送短信。没有文件,我找到解决方案有很多困难。 这是发送短信的脚本,但是我收到错误100002。 首先,我用curl恢复令牌和sessionid,然后再次使用curl发布短信
<?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);
但是我已经制作了另一个脚本来获取已经发送的短信,它确实有效。 这是代码:
<?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);
此链接上使用的3个三个步骤(Sending and receiving SMS by command line with Huawei E3131 and HiLink on a debian system)在命令行上发布sms与e3131模型(我的是e3531)对我有用,但这是使用命令行。 我不能回答这个问题,因为我是这个论坛上的新人。
如何修复此代码?
答案 0 :(得分:2)
您应该在数据请求中添加日期,并关闭日期,此代码对我有用:
<?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);
?>
答案 1 :(得分:1)
由于以下原因,您无法获取所引用设备的令牌的方式:
1-未经批准的URL(不适用于此设备)以获取令牌并发送短信;
2-很快,调用$ xml-> SesInfo方法将不起作用;
但是,我建议您使用file_get_contents函数获取令牌,如下面的脚本所示:
Task.Run(() => RunProcessAndWait());