承接 itabrezshaikh/unirest-php 相关项目开发

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

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

itabrezshaikh/unirest-php

Composer 安装命令:

composer require itabrezshaikh/unirest-php

包简介

Unirest PHP

README 文档

README

Unirest is a set of lightweight HTTP libraries available in multiple languages.

Features

  • Utility methods to call GET, HEAD, POST, PUT, DELETE, CONNECT, OPTIONS, TRACE, PATCH requests
  • Supports form parameters, file uploads and custom body entities
  • Supports gzip
  • Supports Basic, Digest, Negotiate, NTLM Authentication natively
  • Customizable timeout
  • Customizable default headers for every request (DRY)
  • Automatic JSON parsing into a native object for JSON responses

Requirements

Installation

Using Composer

To install unirest-php with Composer, just add the following to your composer.json file:

{
    "require-dev": {
        "apimatic/unirest-php": "2.*"
    }
}

or by running the following command:

composer require apimatic/unirest-php

This will get you the latest version of the reporter and install it. If you do want the master, untagged, version you may use the command below:

composer require apimatic/php-test-reporter:@dev-master

Composer installs autoloader at ./vendor/autoloader.php. to include the library in your script, add:

require_once 'vendor/autoload.php';

If you use Symfony2, autoloader has to be detected automatically.

Install from source

Unirest-PHP requires PHP v5.4+. Download the PHP library from Github, then include Unirest.php in your script:

git clone git@github.com:apimatic/unirest-php.git 
require_once '/path/to/unirest-php/src/Unirest.php';

Usage

Creating a Request

So you're probably wondering how using Unirest makes creating requests in PHP easier, let's look at a working example:

$headers = array("Accept" => "application/json");
$body = array("foo" => "hellow", "bar" => "world");

$response = Unirest\Unirest::post("http://mockbin.com/request", $headers, $body);

$response->code;        // HTTP Status code
$response->headers;     // Headers
$response->body;        // Parsed body
$response->raw_body;    // Unparsed body

File Uploads

To upload files in a multipart form representation use the return value of Unirest\File::add($path) as the value of a parameter:

$headers = array("Accept" => "application/json");
$body = array("file" => Unirest\File::add("/tmp/file.txt"));

$response = Unirest\Unirest::post("http://mockbin.com/request", $headers, $body);

Custom Entity Body

Sending a custom body such as a JSON Object rather than a string or form style parameters we utilize json_encode for the body:

$headers = array("Accept" => "application/json");
$body =   json_encode(array("foo" => "hellow", "bar" => "world"));

$response = Unirest\Unirest::post("http://mockbin.com/request", $headers, $body);

Authentication

First, if you are using Mashape:

// Mashape auth
Unirest\Unirest::setMashapeKey('<mashape_key>');

Otherwise, passing a username, password (optional), defaults to Basic Authentication:

// basic auth
Unirest\Unirest::auth('username', 'password');

The third parameter, which is a bitmask, will Unirest which HTTP authentication method(s) you want it to use for your proxy authentication.

If more than one bit is set, Unirest (at PHP's libcurl level) will first query the site to see what authentication methods it supports and then pick the best one you allow it to use. For some methods, this will induce an extra network round-trip.

Supported Methods

Method Description
CURLAUTH_BASIC HTTP Basic authentication. This is the default choice
CURLAUTH_DIGEST HTTP Digest authentication. as defined in RFC 2617
CURLAUTH_DIGEST_IE HTTP Digest authentication with an IE flavor. The IE flavor is simply that libcurl will use a special "quirk" that IE is known to have used before version 7 and that some servers require the client to use.
CURLAUTH_NEGOTIATE HTTP Negotiate (SPNEGO) authentication. as defined in RFC 4559
CURLAUTH_NTLM HTTP NTLM authentication. A proprietary protocol invented and used by Microsoft.
CURLAUTH_NTLM_WB NTLM delegating to winbind helper. Authentication is performed by a separate binary application. see libcurl docs for more info
CURLAUTH_ANY This is a convenience macro that sets all bits and thus makes libcurl pick any it finds suitable. libcurl will automatically select the one it finds most secure.
CURLAUTH_ANYSAFE This is a convenience macro that sets all bits except Basic and thus makes libcurl pick any it finds suitable. libcurl will automatically select the one it finds most secure.
CURLAUTH_ONLY This is a meta symbol. OR this value together with a single specific auth value to force libcurl to probe for un-restricted auth and if not, only that single auth algorithm is acceptable.
// custom auth method
Unirest\Unirest::proxyAuth('username', 'password', CURLAUTH_DIGEST);

Previous versions of Unirest support Basic Authentication by providing the username and password arguments:

$response = Unirest\Unirest::get("http://mockbin.com/request", null, null, "username", "password");

This has been deprecated, and will be completely removed in v.3.0.0 please use the Unirest\Unirest::auth() method instead

Request Object

Unirest\Unirest::get($url, $headers = array(), $parameters = null)
Unirest\Unirest::post($url, $headers = array(), $body = null)
Unirest\Unirest::put($url, $headers = array(), $body = null)
Unirest\Unirest::patch($url, $headers = array(), $body = null)
Unirest\Unirest::delete($url, $headers = array(), $body = null)
  • url - Endpoint, address, or uri to be acted upon and requested information from.
  • headers - Request Headers as associative array or object
  • body - Request Body as associative array or object

You can send a request with any standard or custom HTTP Method:

$request = Unirest\Unirest::prepare(Unirest\Method::LINK, $url, $headers = array(), $body);

$request = Unirest\Unirest::prepare('CHECKOUT', $url, $headers = array(), $body);

//and then
$response = $request->getResponse();

Response Object

Upon recieving a response Unirest returns the result in the form of an Object, this object should always have the same keys for each language regarding to the response details.

  • code - HTTP Response Status Code (Example 200)
  • headers - HTTP Response Headers
  • body - Parsed response body where applicable, for example JSON responses are parsed to Objects / Associative Arrays.
  • raw_body - Un-parsed response body

Advanced Configuration

You can set some advanced configuration to tune Unirest-PHP:

Custom JSON Decode Flags

Unirest uses PHP's JSON Extension for automatically decoding JSON responses. sometime you may want to return associative arrays, limit the depth of recursion, or use any of the customization flags.

To do so, simply set the desired options using the jsonOpts request method:

Unirest\Unirest::jsonOpts(true, 512, JSON_NUMERIC_CHECK & JSON_FORCE_OBJECT & JSON_UNESCAPED_SLASHES);

Timeout

You can set a custom timeout value (in seconds):

Unirest\Unirest::timeout(5); // 5s timeout

Proxy

Set the proxy to use for the upcoming request.

you can also set the proxy type to be one of CURLPROXY_HTTP, CURLPROXY_HTTP_1_0, CURLPROXY_SOCKS4, CURLPROXY_SOCKS5, CURLPROXY_SOCKS4A, and CURLPROXY_SOCKS5_HOSTNAME.

check the cURL docs for more info.

// quick setup with default port: 1080
Unirest\Unirest::proxy('10.10.10.1');

// custom port and proxy type
Unirest\Unirest::proxy('10.10.10.1', 8080, CURLPROXY_HTTP);

// enable tunneling
Unirest\Unirest::proxy('10.10.10.1', 8080, CURLPROXY_HTTP, true);
Proxy Authenticaton

Passing a username, password (optional), defaults to Basic Authentication:

// basic auth
Unirest\Unirest::proxyAuth('username', 'password');

The third parameter, which is a bitmask, will Unirest which HTTP authentication method(s) you want it to use for your proxy authentication.

If more than one bit is set, Unirest (at PHP's libcurl level) will first query the site to see what authentication methods it supports and then pick the best one you allow it to use. For some methods, this will induce an extra network round-trip.

See Authentication for more details on methods supported.

// basic auth
Unirest\Unirest::proxyAuth('username', 'password', CURLAUTH_DIGEST);

Default Request Headers

You can set default headers that will be sent on every request:

Unirest\Unirest::defaultHeader("Header1", "Value1");
Unirest\Unirest::defaultHeader("Header2", "Value2");

You can do set default headers in bulk:

Unirest\Unirest::defaultHeaders(array(
    "Header1" => "Value1",
    "Header2" => "Value2"
));

You can clear the default headers anytime with:

Unirest\Unirest::clearDefaultHeaders();

SSL validation

You can explicitly enable or disable SSL certificate validation when consuming an SSL protected endpoint:

Unirest\Unirest::verifyPeer(false); // Disables SSL cert validation

By default is true.

Utility Methods

// alias for `curl_getinfo`
Unirest\Unirest::getInfo()

// returns internal cURL handle
Unirest\Unirest::getCurlHandle()

Made with ♥ from the Mashape team

itabrezshaikh/unirest-php 适用场景与选型建议

itabrezshaikh/unirest-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.35k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2016 年 04 月 08 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 itabrezshaikh/unirest-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 itabrezshaikh/unirest-php 我们能提供哪些服务?
定制开发 / 二次开发

基于 itabrezshaikh/unirest-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 12
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-04-08