myseason

ThinkPHP5是国内ThinkPHP团队对老版本进行重构后的匠心之作,其目的是简化PHP开发专注于业务逻辑。本篇文章介绍使用Composer整合HttpClient进行简单的Http操作。

一、安装HttpClient

  本篇教程使用的是PhpStorm IDE它已经内置Composer插件使用相当方便。

php composer.phar require "hightman/httpclient:*"

二、GET请求

public function index(){

    $url = "http://www.cnblogs.com/myseason/";

    $client = new Client();

    $response = $client->get($url);

    if($response->status == 200){
        echo $response->body();
    }
}

三、POST请求

public function index(){

    $url = "http://www.cnblogs.com/myseason/";

    $client = new Client();

    $response = $client->post($url,[\'徐叔\'=>\'科技\']);

    if($response->status == 200){
        echo $response->body();
    }
}

四、自定义Header头

public function index(){
    $client = new Client();

    $request = new Request(\'http://www.cnblogs.com/myseason/\');

    $request->setMethod(\'POST\');

    $request->setHeader(\'user-agent\', \'test robot\');

    $request->setHeader(\'x-server-ip\', \'1.2.3.4\');

    $response = $client->exec($request);
    
    if($response->status == 200){
        echo $response->body;
    }
}

分类:

技术点:

相关文章: