最近做了個小任務,短信、彩信的群發功能。合作方提供了一個接口,我們只要把需要發送的內容拼成一個XML,再http post到那個地址就行。這裡用到了curl這個庫,簡單記錄下用法。
下面是短信群發的小程序,先從一個上傳的電話號碼文本獲取號碼,然後獲取內容發送即可。
<?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;//顯示返回信息
?>
彩信群發則稍稍麻煩些,需要按要求把文本、圖片、mms.smil等文件按規則命名並打包好,但是發送的原理還是一樣的。
<?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;//顯示返回信息
?>
其實很簡單,就是將內容拼成一個XML字符串,按要求base_64編好碼,再post到該地址就行。