Can't get Guzzle working in my Laravel app on Bluemix - php

I have a working PHP app running in Bluemix that I want to extend to call a RESTful service (Insights for Twitter). Since PHP has no built-in way to call the service, I looked around and decided to use Guzzle.
I downloaded Guzzle 6.0.2 from its Git and imported the zip into my httdocs/vendor path and renamed the imported path GuzzleHttp I changed my buildpack to get PHP 5.5 and updated composer.json to the Autoload.psr4 property with:
"GuzzleHttp\\": "htdocs/vendor/"
I redeployed my app and it still worked.
Then I added the following code to my MainController.php: after some other uses:
use GuzzleHttp\Client;
and then later:
$client = new GuzzleHttp\Client([
// Base URI is used with relative requests
'base_uri' => 'https:myserviceURI.mybluemix.net',
// You can set any number of default request options.
'timeout' => 2.0,
]);
// Use guzzle to send a GET request to Watson Twitter Insights
$guzzleresponse = $client->request('GET', '/api/v1/messages/search');
Now, when I redeploy the app I get:
FatalErrorException in HomeController.php line 100:
Class 'App\Http\Controllers\GuzzleHttp\Client' not found
I don't know why it's looking in app\Http\Controllers\ but I tried copying the Guzzle src folder - which includes Client.php - there, renamed it GuzzleHttp and it still fails the same way.
I'm neither a PHP nor a Laravel expert. I inherited the code from an intern team so I don't quite know how all the pieces fit together.
I have some questions:
Did I really need to install Guzzle in my workspace or would it be picked up automatically from the buildpack?
Did I import the Guzzle code in the right way?
Why is it looking for the Guzzle Client in my Controllers path?
Is there a good PHP sample program that drives Insights for Twitter? I found one in Javascript but I need to run this server-side?
And of course, most importantly, what do I need to do to get this working?
Answers to any or all of these questions wold be greatly appreciated

Since you added
use GuzzleHttp\Client;
You have to use Guzzle Client like this:
$client = new Client([
// Base URI is used with relative requests
'base_uri' => 'https:myserviceURI.mybluemix.net',
// You can set any number of default request options.
'timeout' => 2.0,
]);
It tries to look for Guzzle Client in the Controllers path probably because your controller namespace is App\Http\Controllers and you are trying to use Guzzle client like new GuzzleHttp\Client

$client = new Client(array_merge([
'base_uri' => 'URL',
'timeout' => 30.0
]), $options);
/if you need options/
$options = array_merge_recursive([
RequestOptions::AUTH => [
'Conversation_USERNAME',
'CONVERSATION_PASSWORD',
],
RequestOptions::HEADERS => [
'Content-Type' => 'application/json'
]
], $options);

Related

How to use Guzzle HTTP Client in simple PHP?

I am currently using PHP CURL but somehow it is not working for basicAUTH in my case. I want to use guzzel HTTP client in simple PHP project (not a Laravel project). Every solution on the internet I found is setting up in laravel and installing it via composer.
$client = new \GuzzleHttp\Client();
$response = $client->post($service_url, [
'auth' => [
$username,
$admin_password
]
]);
or if guzzel do not works with simple PHP, Suggest other client which can work with simple PHP for basic Auth. Thanks
First of all you need to navigate to your main forder you will be working in, which reside inside htdocs as usual and then insall GuzzleHttpClient by using composer tool
(Recommended: 2+ version)
composer require guzzlehttp/guzzle
It will download and install dependencies such as:
symfony/deprecation-contracts
psr/http-message
psr/http-client
guzzlehttp/promises
ralouphie/getallheaders, etc.
After getting it fully installed; try simple example to see if it really works
<?php
// First of all require autoload from vendor dir;
require_once "vendor/autoload.php";
use GuzzleHttp\Client;
$client = new Client([
// Base URI is used with relative requests
'base_uri' => 'https://reqres.in',
]);
// The response to get
$res = $client->request('GET', '/api/users', [
'query' => ['page' => '2', ]
]);
$body = $res->getBody();
$array_body = json_decode($body);
print_r($array_body);
?>
Check the result in the browser
Composer has nothing to do with Laravel or Guzzle, is "just" a package manager.
You can use - and I suggest to do so - composer in your project even if you're not using a framework.
Said that, once you have Guzzle source code (via composer or downloading directly the code; I would not reccommend the latter) you can just use it everywhere.
By the way I would suggest to use composer for two (main) reasons:
You can keep under control dependencies (like Guzzle) in order to update/downgrade them easily. Also composer will track possible conflicts with other dependencies (that for instance can't work together under some circumstances)
Composer has its own psr-4 autoloader
So yes, you can use Guzzle also without a framework and without composer, there's nothing preventing you to do so.

Use Guzzle in Laravel package development

Recently i am working on a package for Laravel.
I am at beginner level on Laravel Package Development.
I need to use Guzzle Client for an API request from this package.
If i use use GuzzleHttp\Client; in my controller, it's showing class not found. (I know why. because it's outside of app folder and not autoloaded for this path)
Now, how can i use guzzle inside of my custom package controller.
Here what i wanting:
This is my controller method:
public function packageList($vendor, $type)
{
$client = new Client();
$result = $client->get('https://packagist.org/packages/list.json?vendor='.$vendor.'&type='.$type, [
'form_params' => [
'sample-form-data' => 'value'
]
]);
dd($result);
}
I attached classes of Guzzle
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Client;
Getting Exception:
Class 'GuzzleHttp\Client' not found
Found this answer on stack with the same question
Open up your terminal at the root of your project and enter
composer require guzzlehttp/guzzle
It worked for the mailgun API. For some reason, the suggested method at the laravel's mail doc. You may install this package to your project by adding the following line to your composer.json file
"guzzlehttp/guzzle": "~5.3|~6.0"
doesn't make the composer download the Guzzle source codes. By the way, I didn't find out what | means in determining the version. This command just downloads the PSR code.
In this moment, the solution may work. However, be aware of compatibility issues. Because the command would install the latest stable version, not the suitable one.
answer by: shampoo
if this doesnt help, try reading the docs : http://docs.guzzlephp.org/en/stable/quickstart.html

why turn infinity when sending request HTTP GET|POST laravel

I am created new laravel project
composer create-project --prefer-dist laravel/laravel laravel/eagle_project_v2
I istalled guzzle to make http request
composer require guzzlehttp/guzzle
then add this root after created a freelancer Model and controller
Route::apiResource('/freelancers','FreelancersController');
and I want to make request from my controller, but when I call it, turn ininitly without response
-I tried many different url -
$guzzle = new Client;
$result = $guzzle->post('my_url_I_tried_many_URL', [
'form_params' => [
'key' => 'value'
]
]);
return result;
and I tried this without using any library and it make the same error
Although the code is work well when I try it outside laravel (in separate project -only with php-)
Not sure if this will help, but your guzzle post call is to api/freelancers.
Route::apiResources() does not specify that it will use api/ as a prefix. Its purpose is to remove calls to endpoints which are intended to return html (such as create and edit routes).
Unless you specify api as a prefix to your routes, calls to api may not be resolved:
Route::prefix('api')->group(function () {
Route::apiResource('/freelancers','FreelancersController');
});

Laravel guzzle call not working, works fine in postman [duplicate]

I try to make an API Request to the Github API just for testing. I installed the latest Guzzle Version ( "guzzle/guzzle": "^3.9" ) on my Laravel 5.1 APP.
In my routes.php i have the following Code:
Route::get('guzzle/{username}', function($username) {
$client = new Client([
'base_uri' => 'https://api.github.com/users/',
]);
$response = $client->get("/users/$username");
dd($response);
});
If i now visit the URL domain.dev/github/kayyyy i get the Error cURL error 6: Could not resolve host: users.
Why am i getting this Error?
If i visit https://api.github.com/users/kayyyy i can see the json output.
I am also using Homestead / Vagrant is this maybe a Problem that the host cant be resolved?
EDIT
If i try this without the base_uri it works.
Route::get('guzzle/{username}', function($username) {
$client = new GuzzleHttp\Client();
$response = $client->get("https://api.github.com/users/$username");
dd($response);
});
Why are you calling $client->get->()->send()? In particular, why are you chaining the send() method at the end? The documentation does not append the send() method when demonstrating what seems to be the same action:
http://guzzle.readthedocs.org/en/latest/quickstart.html#creating-a-client
Also, did you consider the implications of this statement on the above-cited manual page?
When a relative URI is provided to a client, the client will combine the base URI with the relative URI using the rules described in RFC 3986, section 2.
Actually a variable interpolation is not possible within single quotes. This means that you currently are calling users/$username and the $username variable gets not replaced with its value.
In order to get it, you should use it in one of the following ways:
$response = $client->get("users/$username")->send();
$response = $client->get('users/' . $username)->send();
I personally prefer the second one as it is assumed to be faster.
Okay i solved it, stupid mistake by me.
I used new Client.
And it should be of course new GuzzleHttp\Client
As it is just for testing in my routes.php i did not the Namespace
Thanks for your help everybody.
Thanks to Paratron
https://github.com/googleapis/google-api-php-client/issues/1184#issuecomment-355295789
In my case, on cakephp 3.8 & docker 19.03.5, I was facing curl error due to some network issue. I restarted my cake-server docker container, & it worked like a charm.
Your Laravel installer is very out of date. The only way to get the latest version is to remove and install again:
composer global remove laravel/installer
composer global require laravel/installer

Laravel - Guzzle Request / cURL error 6: Could not resolve host

I try to make an API Request to the Github API just for testing. I installed the latest Guzzle Version ( "guzzle/guzzle": "^3.9" ) on my Laravel 5.1 APP.
In my routes.php i have the following Code:
Route::get('guzzle/{username}', function($username) {
$client = new Client([
'base_uri' => 'https://api.github.com/users/',
]);
$response = $client->get("/users/$username");
dd($response);
});
If i now visit the URL domain.dev/github/kayyyy i get the Error cURL error 6: Could not resolve host: users.
Why am i getting this Error?
If i visit https://api.github.com/users/kayyyy i can see the json output.
I am also using Homestead / Vagrant is this maybe a Problem that the host cant be resolved?
EDIT
If i try this without the base_uri it works.
Route::get('guzzle/{username}', function($username) {
$client = new GuzzleHttp\Client();
$response = $client->get("https://api.github.com/users/$username");
dd($response);
});
Why are you calling $client->get->()->send()? In particular, why are you chaining the send() method at the end? The documentation does not append the send() method when demonstrating what seems to be the same action:
http://guzzle.readthedocs.org/en/latest/quickstart.html#creating-a-client
Also, did you consider the implications of this statement on the above-cited manual page?
When a relative URI is provided to a client, the client will combine the base URI with the relative URI using the rules described in RFC 3986, section 2.
Actually a variable interpolation is not possible within single quotes. This means that you currently are calling users/$username and the $username variable gets not replaced with its value.
In order to get it, you should use it in one of the following ways:
$response = $client->get("users/$username")->send();
$response = $client->get('users/' . $username)->send();
I personally prefer the second one as it is assumed to be faster.
Okay i solved it, stupid mistake by me.
I used new Client.
And it should be of course new GuzzleHttp\Client
As it is just for testing in my routes.php i did not the Namespace
Thanks for your help everybody.
Thanks to Paratron
https://github.com/googleapis/google-api-php-client/issues/1184#issuecomment-355295789
In my case, on cakephp 3.8 & docker 19.03.5, I was facing curl error due to some network issue. I restarted my cake-server docker container, & it worked like a charm.
Your Laravel installer is very out of date. The only way to get the latest version is to remove and install again:
composer global remove laravel/installer
composer global require laravel/installer

Categories