上周, 有 朋友 需要帮忙做一个关于 手机短信验证码注册登录 的功能,之前没有做过,于是 我查查资料,汇总出PHP 接入短信验证码的 3 种方法,现在和大家分享:
1 、 cURL
<?php $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => "https://vip.veesing.com/smsApi/verifyCode", CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => "", CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => "POST", CURLOPT_POSTFIELDS => "appId=41KYR0EB****&appKey=IIWCKKSR7NOQ****&phone=1561894****&templateId=1043&variables=1234", CURLOPT_HTTPHEADER => array( "Content-Type: application/x-www-form-urlencoded;charset=utf-8" ), )); $response = curl_exec($curl); curl_close($curl); echo $response;
2、 HTTP_Request2
<?php
require_once 'HTTP/Request2.php';
$request = new HTTP_Request2();
$request->setUrl('https://vip.veesing.com/smsApi/verifyCode');
$request->setMethod(HTTP_Request2::METHOD_POST);
$request->setConfig(array(
'follow_redirects' => TRUE
));
$request->setHeader(array(
'Content-Type' => 'application/x-www-form-urlencoded;charset=utf-8'
));
$request->addPostParameter(array(
'appId' => '41KYR0EB****',
'appKey' => 'IIWCKKSR7NOQ****',
'phone' => '1561894****',
'templateId' => '1043',
'variables' => '1234'
));
try {
$response = $request->send();
if ($response->getStatus() == 200) {
echo $response->getBody();
}
else {
echo 'Unexpected HTTP status: ' . $response->getStatus() . ' ' .
$response->getReasonPhrase();
}
}
catch(HTTP_Request2_Exception $e) {
echo 'Error: ' . $e->getMessage();
}
3 、 pecl_http
<?php
$client = new http\Client;
$request = new http\Client\Request;
$request->setRequestUrl('https://vip.veesing.com/smsApi/verifyCode');
$request->setRequestMethod('POST');
$body = new http\Message\Body;
$body->append(new http\QueryString(array(
'appId' => '41KYR0EB****',
'appKey' => 'IIWCKKSR7NOQ****',
'phone' => '1561894****',
'templateId' => '1043',
'variables' => '1234')));$request->setBody($body);
$request->setOptions(array());
$request->setHeaders(array(
'Content-Type' => 'application/x-www-form-urlencoded;charset=utf-8'
));
$client->enqueue($request)->send();
$response = $client->getResponse();
echo $response->getBody();
就是这3 种方法,原创不易,请给个三连哦!有疑问可以在评论区交流。
NodeJs - Axios.js、NodeJs - Native.js、NodeJs - Request.js、NodeJs - Unirest.js文件.zip
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/31548553/viewspace-2737557/ ,如需转载,请注明出处,否则将追究法律责任。