Home > Article > Backend Development > HTTP post XML file to a certain address_PHP tutorial
Recently I did a small task, the group sending function of SMS and MMS. The partner provides an interface. We only need to put the content to be sent into an XML, and then http post to that address. The curl library is used here, and the usage is simply recorded.
The following is a small program for mass text messaging. First, get the number from an uploaded phone number text, and then get the content and send it.
<?php if($_FILES['phone_num']['error']>0) { echo 'Problem:'; switch ($_FILES['phone_num']['error']) { //1表示文件超过php配置里的大小限制 case 1: echo 'File exceeded max in phi.ini!';break; //2表示超过最大限制 case 2: echo 'File exceeded max_file_size';break; //3表示部分上传 case 3: echo 'File only partially uploaded';break; //4表示没有上传 case 4: echo 'No file upload'; break; } exit; } // 如果文件类型非纯文本,输出提示 if ($_FILES['phone_num']['type'] != 'text/plain') { echo 'Problem:file is not plain text'; exit; } // 转移文件路径,转移失败,输出错误 $dir = dirname(__file__).'/upload/'; $filename = $_FILES['phone_num']['name']; $savepath = "$dir/$filename"; if (is_uploaded_file($_FILES['phone_num']['tmp_name'])) { $state = move_uploaded_file($_FILES['phone_num']['tmp_name'], $savepath); //如果上传成功,预览 if($state) { //echo "<img src='$filename' alt='$filename' /> "; } /** if (!move_uploaded_file($_FILES['phone_num']['tmp_name'], $savepath)) { echo 'Problem could not move file to destination directory'; exit; } */ } else { echo 'Problem :possible file upload attack file:'; echo $_FILES['phone_num']['name']; exit; } $pn = file_get_contents($savepath); $content = $_POST['content']; $xml_data = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <TaskDataTransfer4EReq xmlns="http://www.aspirehld.com/iecp/TaskDataTransfer4EReq"> <eid>3100</eid> <username>lwxkk</username> <password>1234567</password> <src>106581036177</src> <destmsisdn>'.$pn.'</destmsisdn> <content type="sms"> <title>'.$content.'</title> </content> </TaskDataTransfer4EReq>'; $url = 'http://www.bkjia.com/service/taskSubmit';//接收XML地址 $header = "Content-type: text/xml";//定义content-type为xml $ch = curl_init(); //初始化curl curl_setopt($ch, CURLOPT_URL, $url);//设置链接 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//设置是否返回信息 curl_setopt($ch, CURLOPT_HTTPHEADER, $header);//设置HTTP头 curl_setopt($ch, CURLOPT_POST, 1);//设置为POST方式 curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data);//POST数据 $response = curl_exec($ch);//接收返回信息 if(curl_errno($ch)){//出错则显示错误信息 print curl_error($ch); } curl_close($ch); //关闭curl链接 echo $response;//显示返回信息 ?>
Sending multimedia messages in bulk is a little more troublesome. You need to name and package text, pictures, mms.smil and other files according to the requirements, but the principle of sending is still the same.
<?php // 对彩信包的处理繁琐但是简单,这里省略 $encoded = chunk_split(base64_encode($file_content)); $xml_data = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <TaskDataTransfer4EReq xmlns="http://www.aspirehld.com/iecp/TaskDataTransfer4EReq"> <eid>310</eid> <username>lwxk</username> <password>123456</password> <src>106581036177</src> <destmsisdn>'.$pn.'</destmsisdn> <content type="mms"> <title>'.$title.'</title> <mmsfile>'.$encoded.'</mmsfile> </content> </TaskDataTransfer4EReq>'; $url = 'http://www.bkjia.com/service/taskSubmit';//接收XML地址 $header = "Content-type: text/xml";//定义content-type为xml $ch = curl_init(); //初始化curl curl_setopt($ch, CURLOPT_URL, $url);//设置链接 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//设置是否返回信息 curl_setopt($ch, CURLOPT_HTTPHEADER, $header);//设置HTTP头 curl_setopt($ch, CURLOPT_POST, 1);//设置为POST方式 curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data);//POST数据 $response = curl_exec($ch);//接收返回信息 if(curl_errno($ch)){//出错则显示错误信息 print curl_error($ch); } curl_close($ch); //关闭curl链接 echo $response;//显示返回信息 ?>
In fact, it is very simple, just put the content into an XML string, encode it in base_64 as required, and then post it to the address.