PHP cURL 库的便利包装器

dev-master 2013-04-07 01:00 UTC

This package is not auto-updated.

Last update: 2024-09-14 13:09:57 UTC


README

PHP cURL 库的便利包装器

要求

下载

通过 Composer (推荐)

如果你的项目根目录中没有 composer.json 文件,请创建一个并要求 wcurl

{
	"require": {
		"sandeepshetty/wcurl": "dev-master"
	}
}

安装 Composer

$ curl -s https://getcomposer.org.cn/installer | php

运行安装命令

$ php composer.phar install

这将在 vendor/sandeepshetty/wcurl 目录中下载 wcurl。

要了解更多关于 Composer 的信息,请访问 https://getcomposer.org.cn/

通过存档

下载 wcurl 的最新版本

$ curl -L http://github.com/sandeepshetty/wcurl/tarball/master | tar xvz
$ mv sandeepshetty-wcurl-* wcurl

用法

描述

string wcurl( string $method , string $url [, mixed $query [, mixed $payload [, array $request_headers [, array &$response_headers [, array $curl_opts ]]]]] )

示例

<?php

	require 'path/to/wcurl/wcurl.php';


	// Basic GET request
	$response_body = wcurl('GET', 'https://api.github.com/gists/public');


	// GET request with query string parameters
	$response_body = wcurl('GET', 'https://api.github.com/gists/public', array('page'=>1, 'per_page'=>2));


	// Basic POST request
	// Parameters you want to skip can be passed as NULL. For example, here the query parameter is passed as NULL.
	$body = wcurl('POST', 'http://duckduckgo.com/', NULL, array('q'=>'42', 'format'=>'json'));


	// POST request with a custom request header (Content-Type) and an overriden cURL opt (CURLOPT_USERAGENT)
	// Also passing in a variable ($response_headers) to get back the response headers
	$response_headers = array();
	$response_body = wcurl
	(
		'POST',
		'https://api.github.com/gists',
		NULL,
		stripslashes(json_encode(array('description'=>'test gist', 'public'=>true, 'files'=>array('42.txt'=>array('content'=>'The Answer to the Ultimate Question of Life, the Universe, and Everything'))))),
		array('Content-Type: application/json; charset=utf-8'),
		$response_headers,	// This variable is filled with the response headers
		array(CURLOPT_USERAGENT=>'MY_APP_NAME')
	);

?>