passioncoder/simplecurl

此包已被放弃,不再维护。没有建议的替代包。

为 Laravel 4 设计的一个非常简单且极简的 cURL 包装器

0.1.2 2014-12-07 19:04 UTC

This package is not auto-updated.

Last update: 2020-12-21 08:30:10 UTC


README

为 Laravel 4 设计的一个非常简单且极简的 cURL 包装器。

为什么?

市面上有很多优秀的 PHP HTTP 客户端。但有时你不需要/不需要一个功能齐全的客户端带来的开销,这会迫使你编写 15 行代码来加载 RSS 源或从 REST API 获取数据。

$response = Curl::get('http://example.com/data.json');

SimpleCurl 是这个目的的简单单行命令。它不是打算成为下一个 guzzle,也不会替代 Zend Http Client。 ;)

安装

Composer

passioncoder/simplecurl 添加到你的 composer.json 文件的 require 部分

"require": {
    "passioncoder/simplecurl": "dev-master"
}

Laravel

将服务提供者添加到你的 app/config/app.php

'providers' => array(

    'Passioncoder\SimpleCurl\ServiceProvider',
),

将别名添加到你的 app/config/app.php(可选)

'aliases' => array(

    'Curl'            => 'Passioncoder\SimpleCurl\Facade',
),

使用方法

概要

$response = Curl::get($url, [array $params, [array $options]]);
$response = Curl::post($url, [array $params, [array $options]]);
  • $url: 一个有效的 URL
  • $params: 作为键值对的 GET/POST 参数
  • $options: cURL 选项 作为键值对

示例

try {

	$response = Curl::get('http://example.com/data.json', ['foo' => 'bar'], [CURLOPT_HEADER => false]);

} catch (Passioncoder\SimpleCurl\Exception $e) {
	
	print $e->getMessage();
}

var_dump($response->header);
var_dump($response->body);

if ($response->header->http_code == 200) {
	
	print 'yeah!';
}

许可证

此包是开源软件,许可协议为 MIT 许可证