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

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

Related

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

PHP code working locally but not on Azure

I've implemented a mail delivery service using SparkPost for a website. The code looks like this:
require '/vendor/autoload.php';
use SparkPost\SparkPost; use GuzzleHttp\Client;
use Ivory\HttpAdapter\Guzzle6HttpAdapter;
$httpAdapter = new Guzzle6HttpAdapter(new Client());
$sparky = new SparkPost($httpAdapter, ['key'=>'...']);
[...]
[...]
$results = $sparky->transmission->send($mailarray);
It works just fine locally on WampServer, however when I deploy it to Azure it does not. I don't have access to Azure logs, but I managed to narrow down the problem to one line:
$sparky = new SparkPost($httpAdapter, ['key'=>'...']);
I simply get a 500 error without any other explanation. The weird thing is when I wrap it around a try/catch, I still don't get anything other than a blank screen and a 500 on the console. I suspect it has to do something with /autoload.php not being able to load something.
Any thoughts?
According the requirement of SparkPost lib on Github repo at https://github.com/SparkPost/php-sparkpost/blob/master/composer.json#L18, it need PHP version higher than 5.5. So you can modify the PHP version of your Azure Web Apps, please refer to https://azure.microsoft.com/en-us/documentation/articles/web-sites-php-configure/#how-to-change-the-built-in-php-version for detail steps.

Laravel 5.2 - Class 'GuzzleHttp\Client' not found, on live server only

I have my config/mail 'driver' set to mandrill, host, port, etc. including in services.php mandrill secret set.
Sending Mail works fine locally, but on the live server I get:
Class 'GuzzleHttp\Client' not found
I have tried "guzzlehttp/guzzle": "~5.3|~6.0", and then back to "guzzlehttp/guzzle": "~4.0", no luck. I have done composer update, and dump-autoload still no luck on my live server. Been through every other associated Stackoverflow question and still can't resolve.
Please follow these simple steps:
First: Place this on top of your class
use Guzzlehttp\Client;
Second: Inside your function, instead of using
$client = new Client();
use
$client = new \GuzzleHttp\Client();
reference:
http://docs.guzzlephp.org/en/latest/quickstart.html
try composer require guzzlehttp/guzzle without specifying the version. Worked for me
Error ? the library GuzzleHttp (which is a Simple interface for building query strings, POST requests, streaming large uploads, streaming large downloads, using HTTP cookies, uploading JSON data ) is either missing or mis-configured.
Solution : run the following command on your terminal(root folder):
composer require guzzlehttp/guzzle:^6.5

Laravel: composer says it has installed library and updated composer.json but

but when I go to composer.json, the library ain't there showing.
I just wrote this:
php composer.phar require twilio/sdk
and I could see the typical running of dependencies and versions being installed
and I even updated it to make sure it had actually installed it (otherwise it would complain nothing ain't there) but
when I went to routes and wrote this:
Route::match(array('GET', 'POST'), '/sms', function()
{
$twiml = new Services_Twilio_Twiml();
$twiml->say('Hello - sorry to ring in the WC', array('voice' => 'alice'));
$response = Response::make($twiml, 200);
$response->header('Content-Type', 'text/xml');
return $response;
});
it says it can't find the class, so, either twilio is hiding from every predator or I don't know what.
Question:
So, eventually I would need to know the exact syntax to be added to the config app service providers but I can only find syntax for other libraries related to twilio which are not the official libraries for laravel and I d rather use twilio/sdk and not any other.
What would be the syntax for both the service provider and the alias façade?
Let's se.... And do you know what's the namespace? Because all you need in that case is at the top of the file:
use Path\To\Namespace\Class
and that's all... Or...
$twiml = New Path\To\Namespace\Services_Twilio_Twiml();

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