php如何对接短信接口方法

2021-11-25 10:33:16 admin

PHP短信接口是在开发商城网站、APP、小程序等经常会用到的,比如:用户进行注册、登录以及发送通知类信息。本php短信接口开发实例是基于创信短信接口api而写,从而实现短信发送功能。

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";

//替换成自己的测试账号,参数顺序和wenservice对应

$post_data = "action=send&rt=json&userid=用户ID&account=账号&password=密码&mobile=手机号&content=".rawurlencode("程序调试请测试平时需要发送的正式内容");

//$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字符串并实现自己的逻辑

//0表示成功,其它的参考文档

?>

微信

公众号

服务热线
在线咨询