上周,有朋友需要帮忙做一个关于手机短信验证码注册登录的功能,之前没有做过,于是我查查资料,汇总出PHP接入短信验证码的3种方法,现在和大家分享:
1、cURL
$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
$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种方法,原创不易,请给个三连哦!有疑问可以在评论区交流。