如何在Codeigniter上使用Guzzle? [英] How to use Guzzle on Codeigniter?

查看:165
本文介绍了如何在Codeigniter上使用Guzzle?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Codeigniter 3.2上创建一个Web应用程序,该应用程序可与Facebook Graph API配合使用。为了使GET& POST HTTP请求,我需要用于Codeigniter的卷曲库。我发现了Guzzle,但我不知道如何在Codeigniter上使用Guzzle。

解决方案

查看此链接: $ b https://github.com/rohitbh09/codeigniter-guzzle

  $ this->载入 - >库('guzzle'); 

#guzzle客户端定义
$ client = new GuzzleHttp\Client();

#这个网址定义了guzzle的目标
$ url ='http://www.google.com';

#guzzle
尝试{
#guzzle发送包含表单参数的请求示例
$ response = $ client-> request('POST',
$ url,
['form_params'
=> ['processId'=>'2']
]
);
#guzzle为了将来使用而休憩
echo $ response-> getStatusCode(); // 200
echo $ response-> getReasonPhrase(); // OK
echo $ response-> getProtocolVersion(); // 1.1
echo $ response-> getBody();
} catch(GuzzleHttp\Exception\BadResponseException $ e){
#guzzle为将来使用而休息
$ response = $ e-> getResponse();
$ responseBodyAsString = $ response-> getBody() - > getContents();
print_r($ responseBodyAsString);
}


I am creating a web application on Codeigniter 3.2 which works with the Facebook Graph API. In order to make GET & POST HTTP requests, I need a curl library for Codeigniter. I have found Guzzle but I Don't know how to use Guzzle on Codeigniter.

解决方案

Check this link:

https://github.com/rohitbh09/codeigniter-guzzle

  $this->load->library('guzzle');

  # guzzle client define
  $client     = new GuzzleHttp\Client();

  #This url define speific Target for guzzle
  $url        = 'http://www.google.com';

  #guzzle
  try {
    # guzzle post request example with form parameter
    $response = $client->request( 'POST', 
                                   $url, 
                                  [ 'form_params' 
                                        => [ 'processId' => '2' ] 
                                  ]
                                );
    #guzzle repose for future use
    echo $response->getStatusCode(); // 200
    echo $response->getReasonPhrase(); // OK
    echo $response->getProtocolVersion(); // 1.1
    echo $response->getBody();
  } catch (GuzzleHttp\Exception\BadResponseException $e) {
    #guzzle repose for future use
    $response = $e->getResponse();
    $responseBodyAsString = $response->getBody()->getContents();
    print_r($responseBodyAsString);
  }

这篇关于如何在Codeigniter上使用Guzzle?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆