Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class 'GuzzleHttp\Client' not found

I am using BOTH Guzzle and Codeigniter 3.0 for the first time. Also I admit I am using 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. (The Guzzle docs say nothing about codeigniter).

The Guzzle files are located at 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 am getting

A PHP Error was encountered Severity: Error Message: Class 'GuzzleHttp\Client' not found Filename: controllers/hello.php Line Number: 22 Backtrace:

like image 643
user2115154 Avatar asked Mar 31 '15 18:03

user2115154


3 Answers

In application/config/config.php

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

it work fine for me

like image 133
aitbella Avatar answered Nov 19 '22 02:11

aitbella


You should load it in your controller methods where needed or if desired, autoload it. I use the former: First: use install it using composer in the application folder:

composer require guzzlehttp/guzzle:~6.0

Second: Let CI autoload composer (applications/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;
}

Use:

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

You now have the response in the $your_var variable. For the rest, check the documentation. Otherwise use a "friendlier" method/library for your http requests like CodeIgniter-cURL or Requests

like image 39
wiZZmnma Avatar answered Nov 19 '22 03:11

wiZZmnma


I solved adding following instruction at the beginning of my .php file:

require 'C:/php/vendor/autoload.php';

but I'm not sure it's a good practice...

like image 37
bluish Avatar answered Nov 19 '22 02:11

bluish