承接 novotnyj/http-helper 相关项目开发

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

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

novotnyj/http-helper

Composer 安装命令:

composer require novotnyj/http-helper

包简介

Object oriented cURL in PHP.

关键字:

README 文档

README

This PHP library was created as an alternative to the pecl_http. Goal was to make working with cURL easier and object oriented.

Basic methods has names similar to pecl_http HttpRequest. This should make switching between pecl_http and HttpHelper easier.

HttpHelper\Request is using cURL - make sure that you have cURL extension enabled in php.ini

Requirements

HttpHelper requires PHP 5.3.1 or later with php5-curl installed.

Instalation

HttpHelper can be installed using Composer. Package can be found in Packagist repository.

composer require novotnyj/http-helper

Usage examples

Get request

You can send HTTP request simillar to pecl_http:

use HttpHelper\Request;

$request = new Request("http://www.google.com");
$request->send();
if ($request->getResponseCode() == 200) {
	echo $request->getResponseBody();
}

Or you can use HttpHelper\Response object returned by send method. Also wrap send method in try/catch block in case something went wrong:

...
try {
	$response = $request->send();
	if ($response->getCode() == 200) {
		echo $response->getBody();
	}
} catch (RequestException $e) {
	echo $e->getMessage();
}

Post request

To send a post request change method to POST

use HttpHelper\Request;

$request = new Request("http://www.foo.com/sign/in", Request::POST);
$request->setPostFields(array(
	'login' => 'user',
	'password' => 'p4ssW0rd'
));
try {
	$response = $request->send();
	if ($response->getCode() == 200) {
		echo $response->getBody();
	}
} catch (RequestException $e) {
	echo $e->getMessage();
}

To send a file using POST request:

...
$request->setMethod(Request::POST);
$request->setPostFields(array(
	'upload' => '@/absolute/path/to/file.ext'
));
...

To send a JSON POST request:

...
$request->setMethod(Request::POST);
$request->setJSON(array(
	'login' => 'foo'
));
...

Follow redirects

To enable automatic following Location header (for 301, 302 and 303 response codes):

...
$request->enableRedirects();
...

By default this will follow Location only 20 times. After that RequestException will be raised from send method. You can pass different limit as parameter to enableRedirects (0 means follow Location infinite times). To follow Location for 999 times:

$request->enableRedirects(999);

Note: This function is not using CURLOPT_FOLLOWLOCATION, so you should be fine even with open_basedir or safe_mode in your php.ini

Use response cookies

To enable automatic using of response cookies for next request:

...
$request->enableCookies();
...

NOTE: filtering response cookies by expiration date is not implemented yet.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2014-02-06

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固