短信验证码注册登录的实现,php接入的3种方法(附示例)

码农天地 -
短信验证码注册登录的实现,php接入的3种方法(附示例)

上周,有朋友需要帮忙做一个关于手机短信验证码注册登录的功能,之前没有做过,于是我查查资料,汇总出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/smsAp...');
$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/smsAp...');
$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种方法,原创不易,请给个三连哦!有疑问可以在评论区交流。

PHP - cURL.php、PHP - HTTP_Request2.php、PHP - pecl_http.php文件下载

特别申明:本文内容来源网络,版权归原作者所有,如有侵权请立即与我们联系(cy198701067573@163.com),我们将及时处理。

php介绍

PHP即“超文本预处理器”,是一种通用开源脚本语言。PHP是在服务器端执行的脚本语言,与C语言类似,是常用的网站编程语言。PHP独特的语法混合了C、Java、Perl以及 PHP 自创的语法。利于学习,使用广泛,主要适用于Web开发领域。

Tags 标签

phphttphttps安全

扩展阅读

加个好友,技术交流

1628738909466805.jpg