xionglonghua/yii2-curl

A curl encapsulation for yii2 framework

Installs: 503

Dependents: 1

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 0

Type:yii2-extension

0.0.2 2019-02-26 19:49 UTC

This package is auto-updated.

Last update: 2024-05-27 07:23:22 UTC


README

This extension provides a curl encapsulation for yii2. you can use it for rapid develop;
这个扩展提供了一个基于yii2的curl封装,通过它你能快速的开发。

For license information check the LICENSE-file.
在此处可以查看本扩展的许可

Installation

The preferred way to install this extension is through composer.
推荐的方式是通过composer 进行下载安装composer

Either run
在命令行执行

php composer.phar require --prefer-dist "xionglonghua/yii2-curl" "*"

or add
或加入

"xionglonghua/yii2-curl": "*"

to the require-dev section of your composer.json file.
到你的composer.json文件中的require-dev段。

Usage

Once the extension is installed, simply modify your application configuration as follows:
一旦你安装了这个插件,你就可以直接在配置文件中加入如下的代码:

Now we will use baiduApistore to show how can use it:
现在我们用百度apistore来举例说明你如何使用它:

return [
    'components' => [
        'baiduApi' => [
            'class' => 'xionglonghua\curl\CurlHttp',
            'host' => 'apis.baidu.com',
            'beforeRequest' => function($params, $curlHttp) {
                //you need put you baidu api key here
                $apikey = 'xxxxxx';
                $curlHttp->setHeader('apikey', $apikey);
                return $params;
            },
            'afterRequest' => function($response, $curlHttp) {
                // you may want process the request here, this is just a example
                $code = curl_getinfo($curlHttp->getCurl(), CURLINFO_HTTP_CODE);
                if($code == 200) {
                    $data = json_decode($response, true);
                    if(empty($data) || empty($data['code'])) {
                        Yii::warning("error!", "curl.baidu");
                    }
                    Yii::info("ok!", "curl.baidu");
                    return $response;
                }
                Yii::error("error", "curl.baidu");
                return $response;
            }
            //'protocol' => 'http',
            //'port' => 80,
            // ...
        ],
    ],   
    // .... 
];

After that, you can use it as follow:
在配置好之后,你可以这么访问它:

// you can use this search beijin weather,  http://apistore.baidu.com/apiworks/servicedetail/112.html
$data = Yii::$app->baiduApi
                ->setGet()
                ->httpExec("/apistore/weatherservice/recentweathers", ['cityname' => '北京', 'cityid' => '101010100']);
// you can also use this search the real address of a ip address, http://apistore.baidu.com/apiworks/servicedetail/114.html
$data = Yii::$app->baiduApi
            ->setGet()
            ->httpExec("/apistore/iplookupservice/iplookup", ['ip' => '117.89.35.58']);
// any other apis

as you see, once you configed a api, you can use it anywhere, have fun!
如上所见,一旦你配置好了对接的参数和处理,你就能在任何地方很方便的使用它了,祝您使用愉快!

广告

我们是一群热爱技术,追求卓越的极客,我们乐于做一些对整个社会都有作用的事情,我们希望通过我们的努力来推动整个社会的创新,如果你也一样,欢迎加入我们!