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.
Related
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
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');
});
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);
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
If updated now my symfony project to facebook-sdk-v4. My question is now how to integrate it to use in my controllers?
For the old version i had the following line in my autoload.php
//Loading facebook from vendor
require_once __DIR__ . '/../vendor/facebook/php-sdk/src/facebook.php';
then in the controller i could something like that
$facebook = new \Facebook(array(
'appId' => $this->container->getParameter('fb_appid'),
'secret' => $this->container->getParameter('fb_secret')
));
$fb_userid = $facebook->getUser();
without having any use statement in my controller.
Now with version 4.0 its a bit different. There is no facebook.php file anymore in the vendors, therefore i dont know what to require now. And API call differs too, it looks like
FacebookSession::setDefaultApplication('YOUR_APP_ID', 'YOUR_APP_SECRET');
When i execute that, symfony tells me that it cannot load "FacebookSession" from the global namespace, and if i forget the use statement.
Did anyone have experience how to include the new facebook php sdk in symfony2??
Install the SDK via Composer. Therefore add the following line to the require section of your composer.json and execute composer update afterwards.
"facebook/php-sdk-v4" : "4.0.*"
Facebook SDK will be autoloaded via composer then.