xionglonghua/yii2-curl 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

xionglonghua/yii2-curl

Composer 安装命令:

composer require xionglonghua/yii2-curl

包简介

A curl encapsulation for yii2 framework

README 文档

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!
如上所见,一旦你配置好了对接的参数和处理,你就能在任何地方很方便的使用它了,祝您使用愉快!

广告

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

统计信息

  • 总下载量: 509
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-02-26

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固