kaikna / curli
该包已被废弃,不再维护。未建议替代包。
PHP cUrl 扩展的对象接口
0.9.0
2016-06-17 10:20 UTC
Requires
- php: >=5.4.0
This package is not auto-updated.
Last update: 2020-01-28 20:31:42 UTC
README
轻量级库,具有面向对象的接口,用于发送 HTTP 请求
安装
此包可通过 Composer 获得
{
"require": {
"kajna/curli": "dev-master"
}
}
使用示例
使用 GET 获取 HTML 页面
try { $curli = (new \Curli\Curli()) ->get('http://example.com') ->close(); $response = $curli->response(); echo $response->asText(); } catch(\Exception $e) { echo $e->getMessage(); }
使用 PUT 和连接超时发送和接收 JSON 数据
try { $data = array('foo' => 'bar'); $json = json_encode($data); $curli = (new \Curli\Curli()) ->setConnectionTimeout(3) ->setHeader('Content-Type', 'application/json') ->setHeader('Content-Length', strlen($json)) ->setParams($json) ->put('http://example.com') ->close(); $response = $curli->response(); print_r($response->asObject()); } catch(\Exception $e) { echo $e->getMessage(); }
使用 POST 发送和接收 XML 数据
try { $data = '<root><foo>bar</foo></root>'; $curli = (new \Curli\Curli()) ->setUserAgent('curl 7.16.1 (i386-portbld-freebsd6.2) libcurl/7.16.1 OpenSSL/0.9.7m zlib/1.2.3') ->setHeader('Content-Type', 'text/xml') ->setHeader('Content-Length', strlen($data)) ->setParams($data) ->post('http://example.com') ->close(); $response = $curli->response(); print_r($response->asArray()); } catch(\Exception $e) { echo $e->getMessage(); }
作者
库的作者是 Milos Kajnaco milos@caenazzo.com
许可
Curli 在 MIT 公共许可证下发布。