naruepat / servicecurl
此包最新版本(dev-master)的许可信息不可用。
dev-master
2015-04-30 08:35 UTC
Requires
- php: >=5.4.0
This package is not auto-updated.
Last update: 2024-09-24 04:04:48 UTC
README
#Laravel Service cURL
Naruepat的THiNKNET Web Service的CURL
##安装
在composer.json
中需要此包并更新composer。这将下载该包
"naruepat/servicecurl": "dev-master"
或查看https://packagist.org.cn/packages/naruepat/servicecurl
更新composer后,将ServiceProvider添加到app/config/app.php
中的providers数组
'Naruepat\Servicecurl\ServicecurlServiceProvider',
可以使用外观来缩短代码。将以下内容添加到您的别名
'ServicecURL' => 'Naruepat\Servicecurl\Facades\Servicecurl',
该类绑定到IoC为ServicecURL
$post = ServicecURL::post('www.example.com/list');
或如果配置域,您可以使用路径
$post = ServicecURL::post('list');
配置
首先,再次从命令行运行
Laravel 4的版本
php artisan config:publish naruepat/servicecurl
Laravel 5的版本
php artisan view:publish
以发布默认配置文件。
配置被设计得尽可能灵活。
默认情况下,全局配置可以在app/config/packages/naruepat/servicecurl/config.php
文件中设置。如果没有设置配置,则使用来自vendor/naruepat/servicecurl/src/config/config.php
的包默认值。以下是一个示例配置,显示了所有默认设置
return array( 'domain' => 'http://www.example.com/', 'client_id' => '', 'client_secret' => '' );
##使用方法
<?php // use http method get, post, put, patch, delete $response = ServicecURL::get('http://www.example.com'); $response = ServicecURL::post('http://www.example.com', ['param1' => 'value1']); // easily build an url with a query string $url = ServicecURL::buildUrl('http://www.example.com', ['s' => 'curl']); $response = ServicecURL::get($url); // post() takes an array of POST data $url = ServicecURL::buildUrl('http://api.myservice.com', ['api_key' => 'my_api_key']); $response = ServicecURL::post($url, ['post' => 'data']); // add "json" to the start of the method to post as JSON $response = ServicecURL::jsonPut($url, ['post' => 'data']); // add "raw" to the start of the method to post raw data $response = ServicecURL::rawPost($url, '<?xml version...'); // a response object is returned echo $response->code; // response status code (for example, '200 OK') echo $response->statusCode; // response status code (for example, 200) echo $response->statusText; // response status text (for example, '200 OK') echo $response->body; var_dump($response->headers); // array of headers var_dump($response->info); // array of curl info ?>
如果您需要发送头信息或设置cURL选项,可以操作一个请求对象。使用send()
最终化请求并返回结果。
<?php // newRequest or newJsonRequest returns a Request object $result = ServicecURL::newRequest('post', $url, ['foo' => 'bar']) ->setHeader('content-type', 'application/json') ->setHeader('Accept', 'json') ->setOptions([CURLOPT_VERBOSE => true]) ->send(); ?>