承接 ride/lib-http-client 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ride/lib-http-client

最新稳定版本:1.0.1

Composer 安装命令:

composer require ride/lib-http-client

包简介

HTTP client library of the Ride framework

README 文档

README

HTTP client library of the PHP Ride framework.

What's In This Library

Client

The Client interface lets you implement an HTTP client.

Out of the box, the cURL implementation is provided through the CurlClient class

Request

The Request class adds authentication and other client options to the default HTTP request.

Code Sample

Check the following code sample to see how this library should be used:

<?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
        }
    }
}

Related Modules

You can check the following related modules of this library:

Installation

You can use Composer to install this library.

composer require ride/lib-http-client

统计信息

  • 总下载量: 5.3k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 5
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 7
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-02-21