PHP advanced features: HTTP client and server combat

WBOY
Release: 2024-06-04 16:23:00
Original
410 people have browsed it

PHP provides HTTP client libraries (cURL, GuzzleHttp) for sending HTTP requests, and also supports the creation of HTTP servers (such as Swoole). Practical examples include using cURL to obtain data from the API and using Swoole to create a custom HTTP server to process form data.

PHP advanced features: HTTP client and server combat

PHP Advanced Features: HTTP Client and Server Actual Combat

HTTP Client

PHP has built-in libraries such as cURL and GuzzleHttp, which can be used to create HTTP requests. Here's how to send a GET request using GuzzleHttp:

use GuzzleHttp\Client;

$client = new Client();
$response = $client->get('https://example.com');

// 检索响应状态码
$statusCode = $response->getStatusCode();

// 检索响应正文
$body = $response->getBody()->getContents();
Copy after login

HTTP Server

PHP also allows you to create HTTP servers. The following is a simple Swoole-based server example:

use Swoole\Http\Server;

$server = new Server('0.0.0.0', 8811);
$server->on('request', function (Swoole\Http\Request $request, Swoole\Http\Response $response) {
    $response->header('Content-Type', 'text/plain');
    $response->end('Hello World!');
});

$server->start();
Copy after login

Practical case: API request

The following is a practical case using cURL to retrieve data from an external API:

<?php
$curl = curl_init();

curl_setopt_array($curl, [
    CURLOPT_URL => 'https://api.example.com/v1/users',
    CURLOPT_RETURNTRANSFER => true,
]);

$response = curl_exec($curl);
curl_close($curl);

$data = json_decode($response);

// 处理$data
?>
Copy after login

Practical case: Custom HTTP server

The following is a practical case using Swoole to create a custom HTTP server for simple form processing:

<?php
use Swoole\Http\Server;
use Swoole\Http\Request;

$server = new Server('0.0.0.0', 8812);
$server->on('request', function (Request $request, Swoole\Http\Response $response) {
    // 处理POST数据
    $post = $request->post;

    // 根据要执行的操作创建响应
    if ($post['action'] === 'create') {
        // 处理创建操作
    } elseif ($post['action'] === 'update') {
        // 处理更新操作
    }

    // 发送响应
    $response->end('操作完成');
});

$server->start();
?>
Copy after login

The above is the detailed content of PHP advanced features: HTTP client and server combat. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!