短信驗證碼註冊登陸的實現,php接入的3種方法(附示例)

上週,有朋友須要幫忙作一個關於手機短信驗證碼註冊登陸的功能,以前沒有作過,因而我查查資料,彙總出PHP接入短信驗證碼的3種方法,如今和你們分享:php

一、cURLapp

`<?phpcurl

$curl = curl_init();ui

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(url

"Content-Type: application/x-www-form-urlencoded;charset=utf-8"

),
));.net

$response = curl_exec($curl);code

curl_close($curl);
echo $response;
`orm

二、HTTP_Request2ip

`<?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) {utf-8

echo $response->getBody();

}
else {

echo 'Unexpected HTTP status: ' . $response->getStatus() . ' ' .
$response->getReasonPhrase();

}
}
catch(HTTP_Request2_Exception $e) {
echo 'Error: ' . $e->getMessage();
}`

三、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文件下載

相關文章
相關標籤/搜索