成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

最簡(jiǎn)單輕量的PHP CURL工具庫(kù) ws-http

zengdongbao / 2833人閱讀

摘要:簡(jiǎn)單輕量的客戶端工具庫(kù)可用于測(cè)試支持代理自定義請(qǐng)求頭以及常用請(qǐng)求方法需求安裝使用在文件中新增如下行或者手動(dòng)運(yùn)行命令使用創(chuàng)建一個(gè)請(qǐng)求支持的方法此處給出一些簡(jiǎn)單的實(shí)例請(qǐng)求響應(yīng)

ws-http 簡(jiǎn)單輕量的HTTP 客戶端工具庫(kù)(An Simplified, lightweight HTTP client library)

可用于 HTTP API 測(cè)試,支持 ssl,basic auth,代理,自定義請(qǐng)求頭,以及常用HTTP 請(qǐng)求方法.(An HTTP API testing framework, written in PHP using curl. Supports ssl, basic auth, passing custom request headers, and most HTTP request methods.

--- https://github.com/toohamster...

需求(Requirements)

cURL

PHP 5.4+

安裝(Installation) 使用 (Using) Composer

composer.json文件中新增如下行(To install ws-http with Composer, just add the following to your composer.json file):

{
    "require": {
        "toohamster/ws-http": "*"
    }
}

或者手動(dòng)運(yùn)行命令(or by running the following command):

php composer require toohamster/ws-http
Http Request 使用(Http Request Usage) 創(chuàng)建一個(gè)請(qǐng)求(Creating a Request)
$httpRequest = WsHttpRequest::create();
支持的方法(Support Method)
// set config
$httpRequest->jsonOpts($assoc = false, $depth = 512, $options = 0);
$httpRequest->verifyPeer($enabled);
$httpRequest->verifyHost($enabled);
$httpRequest->verifyFile($file);
$httpRequest->getVerifyFile();
$httpRequest->timeout($seconds);
$httpRequest->defaultHeaders($headers);
$httpRequest->defaultHeader($name, $value);
$httpRequest->clearDefaultHeaders();
$httpRequest->curlOpts($options);
$httpRequest->curlOpt($name, $value);
$httpRequest->clearCurlOpts();
$httpRequest->cookie($cookie);
$httpRequest->cookieFile($cookieFile);
$httpRequest->auth($username = "", $password = "", $method = CURLAUTH_BASIC);
$httpRequest->proxy($address, $port = 1080, $type = CURLPROXY_HTTP, $tunnel = false);
$httpRequest->proxyAuth($username = "", $password = "", $method = CURLAUTH_BASIC);

// http call
$httpRequest->get($url, $headers = [], $parameters = null);
$httpRequest->head($url, $headers = [], $parameters = null);
$httpRequest->options($url, $headers = [], $parameters = null);
$httpRequest->connect($url, $headers = [], $parameters = null);
$httpRequest->post($url, $headers = [], $body = null);
$httpRequest->delete($url, $headers = [], $body = null);
$httpRequest->put($url, $headers = [], $body = null);
$httpRequest->patch($url, $headers = [], $body = null);
$httpRequest->trace($url, $headers = [], $body = null);

此處給出一些簡(jiǎn)單的實(shí)例(Let"s look at a working example):

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

$response = $httpRequest->post("http://mockbin.com/request", $headers, $query);

$response->code;        // 請(qǐng)求響應(yīng)碼(HTTP Status code)
$response->curl_info;   // curl信息(HTTP Curl info)
$response->headers;     // 響應(yīng)頭(Headers)
$response->body;        // 處理后的響應(yīng)消息體(Parsed body)
$response->raw_body;    // 原始響應(yīng)消息體(Unparsed body)
JSON 請(qǐng)求(Requests) (application/json)
$headers = array("Accept" => "application/json");
$data = array("name" => "ahmad", "company" => "mashape");

$body = WsHttpRequestBody::json($data);

$response = $httpRequest->post("http://mockbin.com/request", $headers, $body);

注意(Notes):

Content-Type 會(huì)自動(dòng)設(shè)置成(headers will be automatically set to) application/json

表單請(qǐng)求(Form Requests) (application/x-www-form-urlencoded)
$headers = array("Accept" => "application/json");
$data = array("name" => "ahmad", "company" => "mashape");

$body = WsHttpRequestBody::form($data);

$response = $httpRequest->post("http://mockbin.com/request", $headers, $body);

注意(Notes):

Content-Type 會(huì)自動(dòng)設(shè)置成(headers will be automatically set to) application/x-www-form-urlencoded

Multipart Requests (multipart/form-data)
$headers = array("Accept" => "application/json");
$data = array("name" => "ahmad", "company" => "mashape");

$body = WsHttpRequestBody::multipart($data);

$response = $httpRequest->post("http://mockbin.com/request", $headers, $body);

注意(Notes):

Content-Type 會(huì)自動(dòng)設(shè)置成(headers will be automatically set to) multipart/form-data.

文件上傳(Multipart File Upload)
$headers = array("Accept" => "application/json");
$data = array("name" => "ahmad", "company" => "mashape");
$files = array("bio" => "/path/to/bio.txt", "avatar" => "/path/to/avatar.jpg");

$body = WsHttpRequestBody::multipart($data, $files);

$response = $httpRequest->post("http://mockbin.com/request", $headers, $body);
$headers = array("Accept" => "application/json");
$body = array(
    "name" => "ahmad", 
    "company" => "mashape"
    "bio" => WsHttpRequestBody::file("/path/to/bio.txt", "text/plain"),
    "avatar" => WsHttpRequestBody::file("/path/to/my_avatar.jpg", "text/plain", "avatar.jpg")
);

$response = $httpRequest->post("http://mockbin.com/request", $headers, $body);
自定義消息體(Custom Body)

可以使用WsHttpRequestBody類提供的方法來(lái)生成消息體或使用PHP自帶的序列化函數(shù)來(lái)生成消息體(Sending a custom body such rather than using the WsHttpRequestBody helpers is also possible, for example, using a serialize body string with a custom Content-Type):

$headers = array("Accept" => "application/json", "Content-Type" => "application/x-php-serialized");
$body = serialize((array("foo" => "hello", "bar" => "world"));

$response = $httpRequest->post("http://mockbin.com/request", $headers, $body);
授權(quán)校驗(yàn)(Authentication)
$httpRequest->auth($username, $password, $method);// default is CURLAUTH_BASIC

支持的方法(Supported Methods)

Method Description
CURLAUTH_BASIC HTTP Basic authentication.
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
$httpRequest->proxyAuth("username", "password", CURLAUTH_DIGEST);
Cookies
$httpRequest->cookie($cookie)
$httpRequest->cookieFile($cookieFile)

$cookieFile 參數(shù)必須是可讀取的文件路徑(must be a correct path with write permission).

請(qǐng)求對(duì)象(Request Object)
$httpRequest->get($url, $headers = array(), $parameters = null)
$httpRequest->post($url, $headers = array(), $body = null)
$httpRequest->put($url, $headers = array(), $body = null)
$httpRequest->patch($url, $headers = array(), $body = null)
$httpRequest->delete($url, $headers = array(), $body = null)

url - 請(qǐng)求地址(Endpoint, address, or uri to be acted upon and requested information from)

headers - 請(qǐng)求頭(Request Headers as associative array or object)

body - 請(qǐng)求消息體(Request Body as associative array or object)

可以使用標(biāo)準(zhǔn)的HTTP方法,也可以使用自定義的HTTP方法(You can send a request with any standard or custom HTTP Method):

$httpRequest->send(WsHttpMethod::LINK, $url, $headers = array(), $body);

$httpRequest->send("CHECKOUT", $url, $headers = array(), $body);
響應(yīng)對(duì)象(Response Object)

code - 請(qǐng)求響應(yīng)碼(HTTP Status code)

curl_info - HTTP curl信息(HTTP Curl info)

headers - 響應(yīng)頭(HTTP Response Headers)

body - 處理后的響應(yīng)消息體(Parsed body)

raw_body - 原始響應(yīng)消息體(Unparsed body)

高級(jí)設(shè)置(Advanced Configuration) 自定義json_decode選項(xiàng)(Custom JSON Decode Flags)
$httpRequest->jsonOpts(true, 512, JSON_NUMERIC_CHECK & JSON_FORCE_OBJECT & JSON_UNESCAPED_SLASHES);
超時(shí)設(shè)置(Timeout)
$httpRequest->timeout(5); // 5s timeout
代理(Proxy)

可以設(shè)置代理類型(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
$httpRequest->proxy("10.10.10.1");

// custom port and proxy type
$httpRequest->proxy("10.10.10.1", 8080, CURLPROXY_HTTP);

// enable tunneling
$httpRequest->proxy("10.10.10.1", 8080, CURLPROXY_HTTP, true);
代理授權(quán)驗(yàn)證 (Proxy Authenticaton)
// basic auth
$httpRequest->proxyAuth("username", "password", CURLAUTH_DIGEST);
缺省請(qǐng)求頭 (Default Request Headers)
$httpRequest->defaultHeader("Header1", "Value1");
$httpRequest->defaultHeader("Header2", "Value2");

批量配置(You can set default headers in bulk by passing an array):

$httpRequest->defaultHeaders(array(
    "Header1" => "Value1",
    "Header2" => "Value2"
));

清除配置(You can clear the default headers anytime with):

$httpRequest->clearDefaultHeaders();
缺省Curl選項(xiàng) (Default cURL Options)

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

$httpRequest->curlOpt(CURLOPT_COOKIE, "foo=bar");

批量配置(You can set options bulk by passing an array):

$httpRequest->curlOpts(array(
    CURLOPT_COOKIE => "foo=bar"
));

清除配置(You can clear the default options anytime with):

$httpRequest->clearCurlOpts();
SSL validation
$httpRequest->verifyPeer(false); // Disables SSL cert validation

By default is true.

Http Watcher 使用(Http Watcher Usage) 支持的方法(Support Method)
$watcher = WsHttpWatcher::create($httpResponse);

$watcher->assertStatusCode($assertedStatusCode);
$watcher->assertTotalTimeLessThan($assertedTime);
$watcher->assertHeadersExist(array $assertedHeaders = []);
$watcher->assertHeaders(array $assertedHeaders = []);
$watcher->assertBody($assertedBody, $useRegularExpression = false);
$watcher->assertBodyJson($asserted, $onNotEqualVarExport = false);
$watcher->assertBodyJsonFile($assertedJsonFile, $onNotEqualPrintJson = false);
例子(Examples)
$httpRequest = WsHttpRequest::create();

$httpResponse = $httpRequest->get("https://api.github.com");
$watcher = WsHttpWatcher::create($httpResponse);

$watcher
         ->assertStatusCode(200)
         ->assertHeadersExist(array(
            "X-GitHub-Request-Id",
            "ETag"
         ))
         ->assertHeaders(array(
            "Server" => "GitHub.com"
         ))
         ->assertBody("IS_VALID_JSON")
         ->assertTotalTimeLessThan(2);
$httpRequest = WsHttpRequest::create();
$httpResponse = $httpRequest->get("https://freegeoip.net/json/8.8.8.8");
$watcher = WsHttpWatcher::create($httpResponse);

$watcher
         ->assertStatusCode(200)
         ->assertHeadersExist(array(
            "Content-Length"
         ))
         ->assertHeaders(array(
            "Access-Control-Allow-Origin" => "*"
         ))
         ->assertBodyJsonFile(dirname(__DIR__) . "/tests/Ws/Http/_json/freegeoip.net.json");
查看所有例子(See the full examples) https://github.com/toohamster...

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/21854.html

相關(guān)文章

  • PHP 開發(fā)者應(yīng)了解 24 個(gè)庫(kù)

    摘要:下面是一個(gè)例子這個(gè)庫(kù)要求你至少安裝了和其中的一個(gè),這可能意味著,在大多數(shù)主機(jī)提供商提供的主機(jī)上它可能用不了。借助它,你可以忘記如何書寫乏味的有一個(gè)姊妹庫(kù)叫,是一個(gè)基于的實(shí)現(xiàn)。 showImg(http://segmentfault.com/img/bVbJml); 作為一個(gè)PHP開發(fā)者,現(xiàn)在是一個(gè)令人激動(dòng)的時(shí)刻。每天有許許多多有用的庫(kù)分發(fā)出來(lái),在 Github 上很容易發(fā)現(xiàn)和使用這些庫(kù)...

    0x584a 評(píng)論0 收藏0
  • Ubuntu 14.04 VPS 部署 PHP 環(huán)境及 WordPress

    摘要:軟件及版本選擇是目前用戶數(shù)量數(shù)一數(shù)二的發(fā)行版,背后有大土豪維護(hù),可以說(shuō)是輕量級(jí)用戶的最佳選擇。而是目前最新的版本,目前已經(jīng)發(fā)布了半年了,基本是目前支持最好的版本。是目前官方推薦的最佳的運(yùn)行模式。 軟件及版本選擇 Ubuntu 14.04 Ubuntu 是目前用戶數(shù)量數(shù)一數(shù)二的發(fā)行版,背后有大土豪維護(hù),可以說(shuō)是輕量級(jí)用戶的最佳選擇。而 14.04 是目前最新的 LTS 版本,目前...

    Pluser 評(píng)論0 收藏0
  • PHP編譯參數(shù)configure配置詳解(持續(xù)更新中)

    摘要:編譯參數(shù)使用在源代碼目錄中,該命令可以查看所有編譯參數(shù)以及對(duì)應(yīng)的英文解釋編譯參數(shù)說(shuō)明指定安裝目錄整合指定位置指定額外拓展配置歸放處文件夾打開安全模式打開的支持打開對(duì)的支持打開對(duì) 編譯參數(shù)-使用 ./configure -h在源代碼目錄中,該命令可以查看所有編譯參數(shù)以及對(duì)應(yīng)的英文解釋 編譯參數(shù)-說(shuō)明 --prefix=/opt/php //指定 ...

    hlcc 評(píng)論0 收藏0
  • PHP編譯參數(shù)configure配置詳解(持續(xù)更新中)

    摘要:編譯參數(shù)使用在源代碼目錄中,該命令可以查看所有編譯參數(shù)以及對(duì)應(yīng)的英文解釋編譯參數(shù)說(shuō)明指定安裝目錄整合指定位置指定額外拓展配置歸放處文件夾打開安全模式打開的支持打開對(duì)的支持打開對(duì) 編譯參數(shù)-使用 ./configure -h在源代碼目錄中,該命令可以查看所有編譯參數(shù)以及對(duì)應(yīng)的英文解釋 編譯參數(shù)-說(shuō)明 --prefix=/opt/php //指定 ...

    Lyux 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<