php如何對接短信接口方法
PHP短信接口是在開發(fā)商城網(wǎng)站、APP、小程序等經(jīng)常會用到的,比如:用戶進行注冊、登錄以及發(fā)送通知類信息。本php短信接口開發(fā)實例是基于創(chuàng)信短信接口api而寫,從而實現(xiàn)短信發(fā)送功能。
PHP短信接口demo
<?php
function Post($data, $target) {
$url_info = parse_url($target);
$httpheader = "POST " . $url_info['path'] . " HTTP/1.0\r\n";
$httpheader .= "Host:" . $url_info['host'] . "\r\n";
$httpheader .= "Content-Type:application/x-www-form-urlencoded\r\n";
$httpheader .= "Content-Length:" . strlen($data) . "\r\n";
$httpheader .= "Connection:close\r\n\r\n";
//$httpheader .= "Connection:Keep-Alive\r\n\r\n";
$httpheader .= $data;
$fd = fsockopen($url_info['host'], 80);
fwrite($fd, $httpheader);
$gets = "";
while(!feof($fd)) {
$gets .= fread($fd, 128);
}
fclose($fd);
return $gets;
}
?>
<?php
include_once('sms.php');
$target = "http://dc.28inter.com/sms.aspx";
//替換成自己的測試賬號,參數(shù)順序和wenservice對應
$post_data = "action=send&rt=json&userid=用戶ID&account=賬號&password=密碼&mobile=手機號&content=".rawurlencode("程序調(diào)試請測試平時需要發(fā)送的正式內(nèi)容");
//$binarydata = pack("A", $post_data);
$gets = Post($post_data, $target);
$start=strpos($gets,"<?xml");
$data=substr($gets,$start);
$xml=simplexml_load_string($data);
var_dump(json_decode(json_encode($xml),TRUE));
//請自己解析$gets字符串并實現(xiàn)自己的邏輯
//
?>