当前仓库属于暂停状态,部分功能使用受限,详情请查阅 仓库状态说明
1 Star 0 Fork 52

Ceifei / YurunHttp
暂停

forked from 宇润 / YurunHttp 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

YurunHttp

简介

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

Composer

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

{
    "require": {
        "yurunsoft/yurun-http": "1.3.*"
    }
}

然后执行 composer install 安装。

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

用法

链式调用

<?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/');
if($response->success)
{
    $body = $response->body; // 返回的正文内容
    $header = $response->headers; // 返回头
}
else
{
    // 失败输出错误码和错误信息
    echo $response->errno(), ':', $response->error();
}

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); // 指定文件名保存

文件下载

<?php
$http = HttpRequest::newSession();
$http->download('baidu.html', 'http://www.baidu.com');

断点续传分块下载

<?php
try{
	$download = new Download('http://tool.chinaz.com/ChinazSEOTool.zip');
	$download->blockSize = 1048576; // 每一块数据的大小,可以不设置,默认为1M
	// 绑定每一块数据下载完成事件
	$download->on('progressChanged', function($e){
		var_dump($e);
	});
	// 下载
	$download->download(__DIR__ . '/1.zip');
}catch(Exception $e)
{
	var_dump($e->getMessage());
}

具体详见Demo

The MIT License (MIT) Copyright (c) 2017 宇润 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

一个基于PHP cURL的开源HTTP类库,支持链式操作,省去繁杂的cURL使用方法。 展开 收起
PHP
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
PHP
1
https://gitee.com/hubupas/YurunHttp.git
git@gitee.com:hubupas/YurunHttp.git
hubupas
YurunHttp
YurunHttp
master

搜索帮助

14c37bed 8189591 565d56ea 8189591