2017 © Pedro Peláez
 

library yurun-http

一个基于PHP cURL的开源HTTP类库,支持链式操作,省去繁杂的cURL使用方法。(An open source HTTP class library based on PHP cURL, support chain operations, eliminating the use of miscellaneous cURL methods.)

image

jtunggit/yurun-http

一个基于PHP cURL的开源HTTP类库,支持链式操作,省去繁杂的cURL使用方法。(An open source HTTP class library based on PHP cURL, support chain operations, eliminating the use of miscellaneous cURL methods.)

  • Friday, June 16, 2017
  • by jtunggit
  • Repository
  • 1 Watchers
  • 0 Stars
  • 7 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 30 Forks
  • 0 Open issues
  • 6 Versions
  • 0 % Grown

The README.md

YurunHttp

简介

一个基于PHP cURL的开源HTTP类库,支持链式操作,省去繁杂的cURL使用方法。, (*1)

Composer

本项目可以使用composer安装,遵循psr-4自动加载规则,在你的 composer.json 中加入下面的内容, (*2)

{
    "require": {
        "jtunggit/yurun-http": "dev-master"
    }
}

然后执行 composer install 安装。, (*3)

之后你便可以使用 include "vendor/autoload.php"; 来自动加载类。(ps:不要忘了namespace), (*4)

用法

链式调用

<?php
$http = HttpRequest::newSession();
$response = $http->retry(3) // 失败重试3次
                 ->ua('Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)')
                 ->referer('http://www.baidu.com/')
                 ->accept('text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8')
                 ->acceptLanguage('zh-CN,zh;q=0.8')
                 ->get('http://www.baidu.com/');
var_dump($response);

GET

<?php
$http = HttpRequest::newSession();
$response = $http->get('http://www.baidu.com/');
var_dump($response);

POST(x-www-form-urlencoded)

<?php
$requestBody = array(
    'lang' => 'php',
    'ver'  => 'any'
);
$http = HttpRequest::newSession();
$response = $http->post('http://www.baidu.com/', $requestBody);
var_dump($response);

POST(raw)

<?php
$requestBody = <<<EOF
{
    'lang': 'php',
    'ver':  'any'
}
EOF;
$http = HttpRequest::newSession();
$http->contentType('application/json');
$response = $http->post('http://www.baidu.com/', $requestBody);
var_dump($response);

POST(multi_part)

<?php
$requestBody = new HttpRequestMultipartBody();
$requestBody->add('name', 'php');
$requestBody->addFile('file', '/path/to/aaa.txt', 'aaa.txt');
$http = HttpRequest::newSession();
$response = $http->post('http://www.baidu.com/', $requestBody);
var_dump($response);

FTP下载

<?php
$url = 'ftp://用户名:密码@IP地址/文件路径';
$url = 'ftp://IP地址/文件路径';
$http = HttpRequest::newSession();
// $http->userPwd('用户名','密码'); // 除了在URL里,也可以用这种方式设置密码
$http->saveFile('./')->get($url); // 使用ftp服务器中的文件名保存到当前目录
$http->saveFile('./abc.txt')->get($url); // 指定文件名保存

具体详见Demo, (*5)

The Versions

16/06 2017

dev-master

9999999-dev

一个基于PHP cURL的开源HTTP类库,支持链式操作,省去繁杂的cURL使用方法。(An open source HTTP class library based on PHP cURL, support chain operations, eliminating the use of miscellaneous cURL methods.)

  Sources   Download

MIT

08/06 2017

dev-dev

dev-dev

一个基于PHP cURL的开源HTTP类库,支持链式操作,省去繁杂的cURL使用方法。(An open source HTTP class library based on PHP cURL, support chain operations, eliminating the use of miscellaneous cURL methods.)

  Sources   Download

MIT

08/06 2017

V1.3.3

1.3.3.0

一个基于PHP cURL的开源HTTP类库,支持链式操作,省去繁杂的cURL使用方法。(An open source HTTP class library based on PHP cURL, support chain operations, eliminating the use of miscellaneous cURL methods.)

  Sources   Download

MIT

08/06 2017

V1.3.2

1.3.2.0

一个基于PHP cURL的开源HTTP类库,支持链式操作,省去繁杂的cURL使用方法。(An open source HTTP class library based on PHP cURL, support chain operations, eliminating the use of miscellaneous cURL methods.)

  Sources   Download

MIT

04/06 2017

V1.3

1.3.0.0

一个基于PHP cURL的开源HTTP类库,支持链式操作,省去繁杂的cURL使用方法。(An open source HTTP class library based on PHP cURL, support chain operations, eliminating the use of miscellaneous cURL methods.)

  Sources   Download

MIT

04/06 2017

V1.3.1

1.3.1.0

一个基于PHP cURL的开源HTTP类库,支持链式操作,省去繁杂的cURL使用方法。(An open source HTTP class library based on PHP cURL, support chain operations, eliminating the use of miscellaneous cURL methods.)

  Sources   Download

MIT