ride/lib-http-client

Ride框架的HTTP客户端库

1.0.1 2016-10-12 10:02 UTC

This package is auto-updated.

Last update: 2024-09-13 00:17:17 UTC


README

PHP Ride框架的HTTP客户端库。

本库包含内容

客户端

Client接口允许你实现一个HTTP客户端。

默认情况下,通过CurlClient类提供cURL实现

请求

Request类为默认HTTP请求添加了认证和其他客户端选项。

代码示例

查看以下代码示例以了解如何使用本库

<?php

use ride\library\http\client\CurlClient;
use ride\library\http\client\Client;
use ride\library\http\HttpFactory;
use ride\library\log\Log;

function createHttpClient(HttpFactory $httpFactory, Log $log) {
    $client = new CurlClient($httpFactory);
    
    // optionally set a log, to follow the communication of the client
    $client->setLog($log);

    // set some basic options
    $client->setTimeout(3);
    $client->setFollowLocation(true);
    $client->setUserAgent('My-UserAgent');    

    // use authentication for the requests
    $client->setAuthenticationMethod('basic');
    $client->setAuthenticationMethod('digest');
    $client->setUsername('my-username');
    $client->setPassword('my-password');
    
    // use a proxy
    $client->setProxy('http://url.to/some-proxy);
    
    return $client;
}

function sendRequests(Client $client) {
    // shortcuts for simple requests
    $response = $client->get('http://www.google.com');
    $response = $client->get('http://www.google.com', array('Cache-Control' => 'no-cache'));
    $response = $client->head('http://www.google.com');
    $response = $client->head('http://www.google.com', array('Cache-Control' => 'no-cache'));
    
    // simple post request
    $response = $client->post('http://www.google.com');
    // with body
    $response = $client->post('http://www.google.com', array('q' => 'search string'));
    // and headers
    $response = $client->post('http://www.google.com', array('q' => 'search string'), array('Accept' => '*/*'));
    
    // the same for put and delete
    $response = $client->put('http://www.google.com');
    $response = $client->put('http://www.google.com', array('q' => 'search string'));
    $response = $client->put('http://www.google.com', array('q' => 'search string'), array('Accept' => '*/*'));
    $response = $client->delete('http://www.google.com', array('q' => 'search string'), array('Accept' => '*/*'));
    
    // you can create your own request and tune it before sending it out
    $request = $client->createRequest('https://www.google.com');
    $request->setFollowLocation($followLocation);
    $request->setAuthenticationMethod('basic');
    $request->setUsername('my-username');
    $request->setPassword('my-password');
    
    $response = $client->sendRequest($request);
    
    // handle response
    if ($response->isOk()) {
        $contentType = $response->getHeader('Content-Type');
        $body = $response->getBody();
    } else {
        $statusCode = $response->getStatusCode();
        switch ($statusCode) {
            case 403:
                // forbidden
                break;
            // and more
        }
    }
}

相关模块

你可以查看以下与本库相关的模块

安装

你可以使用Composer来安装此库。

composer require ride/lib-http-client