关于php:短信验证码注册登录的实现php接入的3种方法附示例

5次阅读

共计 2039 个字符,预计需要花费 6 分钟才能阅读完成。

上周,有敌人须要帮忙做一个对于手机短信验证码注册登录的性能,之前没有做过,于是我查查材料,汇总出 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 文件下载

正文完
 0