When writing background code, it is inevitable to interact with other third-party interfaces, such as sending template messages to the service account. Sometimes more than 100,000 messages may need to be sent. At this time, you have to consider using asynchronous and "multi-threaded" network requests.
Today I recommend a Guzzle plug-in to PHP engineers.
Guzzle
Guzzle is an HTTP client for PHP, used to easily send requests and integrate into our WEB services.
The interface is simple: construct query statements, POST requests, split upload and download large files, use HTTP cookies, upload JSON data, etc.
The same interface is used to send synchronous or asynchronous requests.
Use the PSR-7 interface to request, respond, and offload, allowing you to use other compatible PSR-7 class libraries to develop together with Guzzle.
Abstracts the underlying HTTP transport, allowing you to change the environment and other code, such as: not heavily dependent on cURL and PHP streams or sockets, non-blocking event loops.
Middleware systems allow you to create client-side behaviors.
Installing Guzzle
This article combines the Laravel project to introduce the basic use of Guzzle, so it is perfect to use composer to install Guzzle, and the Guzzle official website also recommends using composer to install.
composer require guzzlehttp/guzzle:~6.0 // 或者 php composer.phar require guzzlehttp/guzzle:~6.0
Send a simple POST request
Access third-party interfaces, basically POST requests are the main ones. If you want to make a simple intelligent chat tool, you can use the Turing Robot API to send a POST request to get the automatic answer content. Go directly to the code:
'*****', 'userid' => 'yemeishu' ]; $params['info'] = $request->input('info', '你好吗'); $client = new Client(); $options = json_encode($params, JSON_UNESCAPED_UNICODE); $data = [ 'body' => $options, 'headers' => ['content-type' => 'application/json'] ]; // 发送 post 请求 $response = $client->post('http://www.tuling123.com/openapi/api', $data); $callback = json_decode($response->getBody()->getContents()); return $this->output_json('200', '测试图灵机器人返回结果', $callback); } }
Guzzle client->post function is still very simple Yes, only the access interface and request parameters are required. The parameters mainly include: body, headers, query, etc. For details, please refer to
http://guzzle-cn.readthedocs.io/zh_CN /latest/quickstart.html#id8
Test:
Note: Turing robot is still It's very smart. It can identify the context based on the same userid and achieve intelligent chat.
Send an asynchronous POST request
In PHP development, it is mainly a "process-oriented" development method, but when requesting a third-party interface, sometimes There is no need to wait for the third-party interface to return results before continuing execution. For example, when the user's purchase is successful, we need to send a post request to the SMS interface, and the SMS platform will send a text message to the user to inform the user that the payment was successful, because this type of "reminder message" is an "additional additional function" and is not required. "Know" whether the reminder was sent successfully when the user pays.
At this time, you can use Guzzle's asynchronous request function and look at the code directly:
public function sms(Request $request) { $code = $request->input('code'); $client = new Client(); $sid = '9815b4a2bb6d5******8bdb1828644f2'; $time = '20171029173312'; $token = 'af8728c8bc*******12019c680df4b11c'; $sig = strtoupper(md5($sid.$token.$time)); $auth = trim(base64_encode($sid . ":" . $time)); $params = ['templateSMS' => [ 'appId' => '12b43**********0091c73c0ab', 'param' => "coding01,$code,30", 'templateId' => '3***3', 'to' => '17689974321' ] ]; $options = json_encode($params, JSON_UNESCAPED_UNICODE); $data = [ 'query' => [ 'sig' => $sig ], 'body' => $options, 'headers' => [ 'content-type' => 'application/json', 'Authorization' => $auth ] ]; // 发送 post 请求 $promise = $client->requestAsync('POST', 'https://api.ucpaas.com/2014-06-30/Accounts/9815b4a2bb6d5******8bdb1828644f2/Messages/templateSMS', $data); $promise->then( function (ResponseInterface $res) { Log::info('---'); Log::info($res->getStatusCode() . "\n"); Log::info($res->getBody()->getContents() . "\n"); }, function (RequestException $e) { Log::info('-__-'); Log::info($e->getMessage() . "\n"); } ); $promise->wait(); return $this->output_json('200', '测试短信 api', []); }
Return the interface data first:
Then output Log:
[2017-10-29 09:53:14] local.INFO: --- [2017-10-29 09:53:14] local.INFO: 200 [2017-10-29 09:53:14] local.INFO: {"resp":{"respCode":"000000","templateSMS":{"createDate":"20171029175314","smsId":"24a93f323c9*****8608568"}}}
The last SMS message received:
Send multi-threaded asynchronous POST request
「Send "Multi-threaded asynchronous POST request" is used in many occasions. For example: Double Eleven is coming soon. You can do some activities to give back to old users. This requires batch push of a template message to old users to tell them which activities to participate in. of. At this time, you need to use multi-threaded asynchronous request WeChat official account interface.
Go directly to the code:
public function send($templateid, $openid, $url, $data) { $client = $this->bnotice->getHttp()->getClient(); $requests = function ($open_ids) use ($templateid, $url, $data) { foreach($open_ids as $v){ try { yield $this->bnotice ->template($templateid) ->to($v) ->url($url) ->data($data) ->request(); } catch(Exception $e) { Log::error('sendtemplate:'.$e->getMessage()); } } }; $pool = new Pool($client, $requests($openid), [ 'concurrency' => 16, 'fulfilled' => function ($response, $index) { }, 'rejected' => function ($reason, $index) { }, ]); $promise = $pool->promise(); $promise->wait(); }
The request method:
public function request($data = []) { $params = array_merge([ 'touser' => '', 'template_id' => '', 'url' => '', 'topcolor' => '', 'miniprogram' => [], 'data' => [], ], $data); $required = ['touser', 'template_id']; foreach ($params as $key => $value) { if (in_array($key, $required, true) && empty($value) && empty($this->message[$key])) { throw new InvalidArgumentException("Attribute '$key' can not be empty!"); } $params[$key] = empty($value) ? $this->message[$key] : $value; } $params['data'] = $this->formatData($params['data']); $this->message = $this->messageBackup; $options = json_encode ( $params, JSON_UNESCAPED_UNICODE); $data = [ 'query' => [ 'access_token' => $this->getAccessToken()->getToken() ], 'body' => $options, 'headers' => ['content-type' => 'application/json'] ]; return function() use ($data) { return $this->getHttp()->getClient()->requestAsync('POST', $this::API_SEND_NOTICE, $data); }; }
Guzzle multi-threaded asynchronous request prototype function, using the GuzzleHttp\Pool object
use GuzzleHttp\Pool;use GuzzleHttp\Client;use GuzzleHttp\Psr7\Request;$client = new Client();$requests = function ($total) { $uri = 'http://127.0.0.1:8126/guzzle-server/perf'; for ($i = 0; $i < $total; $i++) { yield new Request('GET', $uri); }};$pool = new Pool($client, $requests(100), [ 'concurrency' => 5, 'fulfilled' => function ($response, $index) { // this is delivered each successful response }, 'rejected' => function ($reason, $index) { // this is delivered each failed request },]);// Initiate the transfers and create a promise$promise = $pool->promise();// Force the pool of requests to complete.$promise->wait();
Summary
With Guzzle, it is greatly convenient for us to request third-party interfaces concurrently and asynchronously. If time permits, we can take a look at the Guzzle source code to see how it is implemented.
Recommended tutorial: "PHP Tutorial"
The above is the detailed content of PHP network request plug-in Guzzle use. For more information, please follow other related articles on the PHP Chinese website!