comodojo/httprequest 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

comodojo/httprequest

最新稳定版本:1.3.0

Composer 安装命令:

composer require comodojo/httprequest

包简介

HTTP request library

README 文档

README

Build Status Latest Stable Version Total Downloads Latest Unstable Version License Scrutinizer Code Quality Code Coverage

HTTP request library

This is the development branch, please do not use it in production

Main features:

  • BASIC, NTLM, DIGEST and SPNEGO auth (requires php curl library) authentication support
  • proxy support
  • allowed http methods: GET, POST, PUT, DELETE
  • CURL or stream working mode
  • request customization (useragent, http version, content type, ...)

Installation

Install composer, then:

composer require comodojo/httprequest

Basic usage

Library usage is trivial: first create an instance of Httprequest specifing remote host address, then use get or send method to start request. It's important to wrap code in a try/catch block to handle exceptions (if any).

Constructor accepts two parameters: remote host address (required) and a boolean value (optional) that, if false, will force lib to use streams instead of curl.

  • Using get:

    try {
    
        // create an instance of Httprequest
        $http = new \Comodojo\Httprequest\Httprequest("www.example.com");
    
        // or:
        // $http = new \Comodojo\Httprequest\Httprequest();
        // $http->setHost("www.example.com");
    
        // get remote data
        $result = $http->get();
    
    } catch (\Comodojo\Exception\HttpException $he) {
    
    	/* handle http specific exception */
    
    } catch (\Exception $e) {
    
    	/* handle generic exception */
    
    }
  • Using send:

    $data = array('foo'=>'bar', 'baz'=>'boom');
    
    try {
    
        // create an instance of Httprequest
        $http = new \Comodojo\Httprequest\Httprequest("www.example.com");
    
        // get remote data
        $result = $http->setHttpMethod("POST")->send($data);
    
    } catch (\Comodojo\Exception\HttpException $he) {
    
    	/* handle http specific exception */
    
    } catch (\Exception $e) {
    
    	/* handle generic exception */
    
    }

Class setters (chainable methods)

  • Set destination port (default 80)

    $http->setPort(8080);
  • Set timeout (in secs)

    $http->setTimeout(10);
  • Set a custom user agent (default to 'Comodojo-Httprequest')

    $http->setUserAgent("My-Custom-User-Agent");
  • Set HTTP version (1.0 or 1.1)

    $http->setHttpVersion("1.1");
  • Set content type (default to 'application/x-www-form-urlencoded' and used only with send method)

    $http->setContentType("multipart/form-data");
  • Set additional/custom headers:

    $http->setHeader("My-Header","foo");
  • Set authentication:

    // NTLM
    $http->setAuth("NTLM", "myusername", "mypassword");
    
    // BASIC
    $http->setAuth("BASIC", "myusername", "mypassword");
  • Set proxy:

    // No authentication
    $http->setProxy(proxy.example.org);
    
    // Authentication
    $http->setProxy(proxy.example.org, "myusername", "mypassword");
  • Ignore errors (stream only):

    Force the stream to ignore errors and to return http code and content from the server instead of throwing an exception.

    // Set the stream to ignore errors
    $http->setIgnoreErrors(true);

Class getters

  • Get response headers:

    // After a request...
    
    $headers = $http->getReceivedHeaders();
  • Get HTTP received status code:

    // After a request...
    
    $code = $http->getHttpStatusCode();

Multiple requests

The reset method helps resetting options and data channel; for example:

try {

    // create an instance of Httprequest
    $http = new \Comodojo\Httprequest\Httprequest();

    // first request
    $first_data = $http->setHost("www.example.com")->get();

    // channel reset
    $http->reset();

    // second request
    $second_data = $http->setHost("www.example2.com")->setHttpMethod("POST")->send(array("my"=>"data"));

} catch (\Comodojo\Exception\HttpException $he) {

	/* handle http specific exception */

} catch (\Exception $e) {

	/* handle generic exception */

}

Documentation

Contributing

Contributions are welcome and will be fully credited. Please see CONTRIBUTING for details.

License

comodojo/httprequest is released under the MIT License (MIT). Please see License File for more information.

统计信息

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

GitHub 信息

  • Stars: 2
  • Watchers: 3
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-07-22