guzzle 5.0 postAsync error in laravel 5.0 - laravel-5

Hi I am trying to use Guzzle in my laravel application to send some data to a remote application. When i try to use $client->post('url') it works fine but whenever i use any of async functions like postAsync, sendAsync i get the BadMethodException.
local.ERROR: exception 'BadMethodCallException' with message 'Unknown method, sendAsync' in /home/lycan/Documents/workspace/my_project/vendor/guzzlehttp/guzzle/src/functions.php:324
My method is below
private function sendDataToRemoteApplication()
{
$client = new \GuzzleHttp\Client();
$url='http://localhost:9000/submitResults/?key1=valu1&key2=22';
$request = new Request('POST', $url);
$promise = $client->sendAsync($request)->then(function ($response) {
echo 'I completed! ' . $response->getBody();
});
$promise->wait();
}
i followed everything from the documents so it should work !
Tried to find out the solution on google but didn't find a link where any one faced this issue....( i would consider my self lucky if i am the first to face this issue though :D ) . Can one please suggest what is the problem.

You may try this (\GuzzleHttp\Psr7\Request):
$request = new \GuzzleHttp\Psr7\Request('GET', 'http://example.com');
$promise = $client->sendAsync($request)->then(function ($response) {
echo 'I completed! ' . $response->getBody();
});
$promise->wait();

Related

The image cannot be displayed because it contains errors laravel

I am working on a laravel project in which I am trying to show image using url but getting error. I have tried a lot and search everything but I don't understand why I am getting this error. I am using the below function
public function displayImage($filename){
$path = storage_path("app/taskImage/".$filename);
if (!File::exists($path)) {
abort(404);
}
$file = File::get($path);
$type = File::mimeType($path);
$response = \Response::make($file, 200);
$response->header("Content-Type", $type);
return $response;
}
And the route is
Route::get('taskImg/{filename?}', [
'uses' => 'FormController#displayImage',
]);
And the URL which I am tring is like
http://localhost/project_name/public/taskImg/test.jpg
when I have print something I am getting the that but Not getting the Image. It is showing a blank screen with error message The image cannot be displayed because it contains errors
you can do it like this way
$storagePath = storage_path("app/taskImage/".$filename);
return Image::make($storagePath)->response();
I've spent a LOT of hours searching for a solution for this, I found it at last! just add ob_end_clean before returning the response, like the following:
ob_end_clean();
return response()->file($path,['Content-type' => 'image/jpeg']);
I can't explain because I don't know what was going on, any one could explain this?

problem with multiple HTTP requests in Laravel with Guzzle

I'm using Guzzle version 6.3.3. I want to make multiple HTTP requests from an external API. The code shown below worker perfect for me. This is just one single request.
public function getAllTeams()
{
$client = new Client();
$uri = 'https://api.football-data.org/v2/competitions/2003/teams';
$header = ['headers' => ['X-Auth-Token' => 'MyKey']];
$res = $client->get($uri, $header);
$data = json_decode($res->getBody()->getContents(), true);
return $data['teams'];
}
But now I want to make multiple requests at once. In the documentation of Guzzle I found out how to do it, but it still didn't work properly. This is the code I try to use.
$header = ['headers' => ['X-Auth-Token' => 'MyKey']];
$client = new Client(['debug' => true]);
$res = $client->send(array(
$client->get('https://api.football-data.org/v2/teams/666', $header),
$client->get('https://api.football-data.org/v2/teams/1920', $header),
$client->get('https://api.football-data.org/v2/teams/6806', $header)
));
$data = json_decode($res->getBody()->getContents(), true);
return $data;
I get the error:
Argument 1 passed to GuzzleHttp\Client::send() must implement interface Psr\Http\Message\RequestInterface, array given called in TeamsController.
If I remove the $header after each URI then I get this error:
resulted in a '403 Forbidden' response: {"message": "The resource you are looking for is restricted. Please pass a valid API token and check your subscription fo (truncated...)
I tried several ways to set X-Auth-Token with my API key. But I still get errors and I don't know many other ways with Guzzle to set them.
I hope someone can help me out :)
Guzzle 6 uses a different approach to Guzzle 3, so you should use something like:
use function GuzzleHttp\Promise\all;
$header = ['headers' => ['X-Auth-Token' => 'MyKey']];
$client = new Client(['debug' => true]);
$responses = all([
$client->getAsync('https://api.football-data.org/v2/teams/666', $header),
$client->getAsync('https://api.football-data.org/v2/teams/1920', $header),
$client->getAsync('https://api.football-data.org/v2/teams/6806', $header)
])->wait();
$data = [];
foreach ($responses as $i => $res) {
$data[$i] = json_decode($res->getBody()->getContents(), true);
}
return $data;
Take a look at different questions on the same topic (#1, #2) to see more usage examples.

Laravel 5.8 post call in curl works great but not with guzzle

When I perform this post call in my terminal with curl everything works great I see the post call coming in:
my curl call:
curl -X POST https://requestloggerbin.herokuapp.com/bin/a4d73cbb-2ddc-4fc7-ac38-60c2fac7e015 -d '{"test": "foo"}'
I am trying to replicate this call in my laravel app with guzzle, but I don't see the post call coming in and I get no error messages whatsoever so I have no idea what's going wrong.
My guzzle call:
$client = new Client();
$request = $client->post(
'https://requestloggerbin.herokuapp.com/bin/a4d73cbb-2ddc-4fc7-ac38-60c2fac7e015',
['body' => ['foo' => 'bar']]
);
$response = $request->send();
What am I doing wrong here?
$response = $request->send();
Which is not required at all .
use GuzzleHttp\Client;
$client = new Client();
$response = $client->post('http://localhost.com/23', ['body' => $requestXmlBody]);
$result = $response->getBody()->getContents();
$result1 = simplexml_load_string($result);

How to post and receive data on my Laravel API with Guzzle

I have a very awkward issue here:
I am trying to call my own Laravel API like this:
$api_url `enter code here`= env("API_URL")."login";
$data= json_encode($data);
$api_url = env("API_URL")."login";
$client = new \GuzzleHttp\Client();
$headers = array('Accept: application/json','Content-Type: application/json', 'X-XSRF-TOKEN' => csrf_token());
$response = $client->request('POST', $api_url , ['headers'=>$headers,'json' => $data]);
print_r($response);
Then my API end point has code like this:
public function login(Request $request)
{
//This line gives me NULL
dump($request->all());
//the lines below print this "{"email":"dev#gumption.pk","password":"password01"}"
//but I cannot get this POSTED data in variables even with $request->input('email')
$content = $request->getContent();
print_r($content);
}
I am NOT able to retrieve variables from this POSTED data. I have tried $request->input('email');
$request->json('email');
Nothing is working. It simply says NULL.
I am not sure what is wrong here.

Laravel 4 - Artadarek/OAuth-4-laravel issue

Hi i am laravel 4 begginer.
i try to use Artadek/OAuth-4-laravel package
Everything went well untill using the example what i did:
Route:
Route::get('lfb', array(
'as' => 'lfb',
'uses' => 'HomeController#signInWithFacebook'
));
Controller:
class HomeController extends BaseController {
public function signInWithFacebook() {
// get data from input
$code = Input::get( 'code' );
// get fb service
$fb = OAuth::consumer( 'Facebook' );
// check if code is valid
// if code is provided get user data and sign in
if ( !empty( $code ) ) {
// This was a callback request from facebook, get the token
$token = $fb->requestAccessToken( $code );
// Send a request with it
$result = json_decode( $fb->request( '/me' ), true );
$message = 'Your unique facebook user id is: ' . $result['id'] . ' and your name is ' . $result['name'];
echo $message. "<br/>";
//Var_dump
//display whole array().
dd($result);
}
// if not ask for permission first
else {
// get fb authorization
$url = $fb->getAuthorizationUri();
// return to facebook login url
return Redirect::to( (string)$url );
}
}
And blade :
#extends('layout.main')
#section('content')
Sign in with Facebook
#stop
Last error log from laravel.log:
Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException
open: F:\wamp\www\IbidsLR\bootstrap\compiled.php
{
$routes = $this->get($request->getMethod());
$route = $this->check($routes, $request);
if (!is_null($route)) {
return $route->bind($request);
}
$this->checkForAlternateVerbs($request);
throw new NotFoundHttpException();
}
protected function checkForAlternateVerbs($request)
And i get when press on the button "Whoops,somthing went wrong." and it redirect me to lfb page which i dont have any page called that name i just want to use fb login..
What wrong here?
Goes to your laravel folder, then open app/config/app.php and make sure 'debug' => 'true' and it will help you showing what kinds of error you are having now.
It is working fine with your routes and also signInWithFacebook() function. Make sure that you already created a config file for Artadek/OAuth-4-laravel package, and it will be in app/config/packages.
It is showing lfb page because that is your route -- your function will be working with it.
Please make sure debug is on and check your error again. It is hard to explain with just that 'Whoops, something went wrong.'

Resources