Class "GuzlHttp \ Client" not found

I am using BOTH Guzzle and Codeigniter 3.0 for the first time. I also assume that I am using the php namespace for the first time.

I am trying to make a very simple get request using Guzzle according to the examples provided in the docs. (Guzzle docs don't say anything about coderiger).

Guzzle files are located in application / class / guzzle

Here is my very simple controller

public function indey () { $data = array(); $data['main_content'] = "hiview"; $data['title'] = "Data Analyzer - Welcome"; $data['xas'] = $this->guzzler(); $this->load->view('template', $data); } private function guzzler() { $client = new GuzzleHttp\Client; $response = $client->get('http://guzzlephp.org'); return $response; } 

This is my simple view.

  <div class="row"> <div class="col-xs-12"> <h1>Hi</h1> </div> </div> <div class="row"> <div class="col-xs-12"> <h1><?php var_dump($xas); ?></h1> </div> </div> 

This is the error I get

A PHP error has occurred Severity level: Error Message: Class 'GuzzleHttp \ Client' not found File name: controllers / hello.php Line number: 22 Backtrace:

+7
php codeigniter guzzle
source share
2 answers

You must load it in your controller methods, where it is necessary or, if desired, autoload. I use the first: First: use it using the composer in the application folder:

 composer require guzzlehttp/guzzle:~6.0 

Second: let CI autoload composer (application / config / config.php)

 $config['composer_autoload'] = TRUE; 

Then in your controller

  public function guzzler_get($url, $uri) { $client = new GuzzleHttp\Client(['base_uri' => $url]); $response = $client->get($uri); // print_r($response); // print out response // print out headers: // foreach ($response->getHeaders() as $name => $values) { // echo $name . ': ' . implode(', ', $values) . "\r\n"; // } return $response; } 

Using:

 $your_var = $this->guzzler_get('http://httpbin.org', '/html'); 

Now you have the answer in the variable $your_var . Otherwise check the documentation. Otherwise, use a "friendly" method / library for your HTTP requests, such as CodeIgniter-cURL or Requests

+3
source

In application/config/config.php

  $ config ['composer_autoload'] = FCPATH.'vendor / autoload.php ';

it works great for me

+3
source

All Articles