Accessing a URL stored in a variable from a controller - php

I am in a confusing situation
In my Laravel controller, I have a variable
public function storeName($key)
$store = new Store();
$storeName = $store->connectAPI($key);
This $storeName variable will actually give me a URL, which if accessed, will give me a JSON response.
If I die and dump $storeName variable it will print
http://store123.com?key=2093983892
But, what I actually want is to access this $storeName variable, by passing a GET request in my controller, so I can get a JSON response from this API call.
How can I access this URL in my controller?

From Guzzle docs
$client = new GuzzleHttp\Client();
$res = $client->request('GET', 'https://api.github.com/user', []);
if($res->getStatusCode())
{
// "200"
$json = $res->getBody();
}

I used json_decode inside a curl_init function to solve this issue.

Related

Laravel HTTP Client - method chaining issue

I have been using the HTTP client in Laravel 8, as follows:
$http = Http::asForm()->post($url, $post_data);
$response = $http->body();
This works great. Now I want to include a file upload as part of this request, but the file is optional. I have tried to structure my request like this:
use Illuminate\Support\Facades\Http;
use Illuminate\Http\Request;
public function index(Request $request)
{
$url = 'my-url';
$post_data = $request->post();
$http = Http::asForm();
if ($request->hasFile('image')) {
$http->attach('image', $request->file('image'));
}
$http->post($url, $post_data);
$response = $http->body();
}
But this is not working. The error I am getting is: Method Illuminate\Http\Client\PendingRequest::body does not exist.
The post() method appears to be returning Illuminate\Http\Client\PendingRequest instead of Illuminate\Http\Client\Response.
Any ideas how to do this?
$response = $http->post($url, $post_data)->body();
// alternatively:
$pendingRequest = $http->post($url, $post_data);
$response = $pendingRequest->body();
In your second code sample, you don't assign the return of post() to a variable. But this return value (Illuminate\Http\Client\PendingRequest) is the object that defines the body() method. So either use a second variable (or reassign $http), or chain the post() and body() calls as shown above.

Storing response from PHP Guzzle as JSON in database (Laravel)

Im calling a vehicle data API at an endpoint and can return the data fine using:
$client = new Client();
$url = "https://uk1.ukvehicledata.co.uk/api/MISC DETAILS;
$result = $client->get($url);
return $result->request;
This is using mock data so the response is:
{
"Request":{
"RequestGuid":"",
"PackageId":""
"Response":{
// VEHICLE DATA
}
}
However, I now wish to store the response in the database but cannot access ->request or ->Request etc.
Using:
return json_encode( (array)$result );
Actually returns the headers from Guzzle and no response data.
Any help?
You have to convert the body into JSON first:
$jsonArray = json_decode($result->getBody()->getContents(), true);
echo $jsonArray['Request']['RequestGuid'];

Accessing specific properties from Guzzle response?

I'm using Guzzle to get an HTTP response. If I do this:
$response = $res->getBody();
I get an object with 'email' as one of the properties. But if I do either:
$email = $res->getBody()->email;
or
$email = $response->email
I get a 'No value for email' error. What am I missing?? How can I access a specific property in the response object?
The getBody method returns an instance of StreamInterface. You first need to retrieve the contents of the response:
$response = (string) $res->getBody();
Only then you can decode the json payload:
$json = json_decode($response);
$email = $json->email;

Symfony2: UnitTests for AJAX Controllers

I'm going to write some Symfony2 UnitTests (derived from Symfony\ Bundle\ FrameworkBundle\ Test\ WebTestCase) to test ajax controllers, similar to this How to get Ajax post request by symfony2 Controller.
My big problem is to get the parameters into the "request" bag of the request, not into the "parameter" bag. Similar to the upper example the method in the controller looks like this:
public function ajaxAction(Request $request)
{
$data = $request->request->get('data');
}
But if i do a var_dump of the $request, the paramaters i supply in the WebTestCase do not appear in $request->request, but in $request->parameter. Let's say this is the portion of code in my webtestcase:
....
$client = static::createClient();
$client->request('POST', '/ajax/blahblah', ... ?????);
I already tried supplying the parameter(s) directly within the url as
/ajax/blahblah?data=whocares
I tried specifying the parameter within an array
$client->request('POST', '/ajax/blahblah', array('data' => 'fruityloops'));
But nothing worked. Any chance to get this running?
Thanks in advance
Hennes
After you make the request, you need to get the response. Try this:
$client = static::createClient();
$client->request('POST', '/ajax/blahblah', array('data' => 'fruityloops'));
$response = $client->getResponse();
$this->assertEquals(200, $response->getStatusCode());
//convert to array
$data = json_decode($response->getContent(true), true);
var_dump($data);
$this->assertArrayHasKey('your_key', $data);

Access Guzzle Response from Goutte

I'm trying to access to the Guzzle Response object from Goutte. Because that object has nice methods that i want to use. getEffectiveUrl for example.
As far as i can see there is no way doing it without hacking the code.
Or without accessing the response object, is there a way to get the last redirected url froum goutte?
A little late, but:
If you are only interested in getting the URL you were last redirected to, you could simply do
$client = new Goutte\Client();
$crawler = $client->request('GET', 'http://www.example.com');
$url = $client->getHistory()->current()->getUri();
EDIT:
But, extending Goutte to serve your needs is fairly easy. All you need is to override the createResponse() method and store the GuzzleResponse
namespace Your\Name\Space;
class Client extends \Goutte\Client
{
protected $guzzleResponse;
protected function createResponse(\Guzzle\Http\Message\Response $response)
{
$this->guzzleResponse = $response;
return parent::createResponse($response);
}
/**
* #return \Guzzle\Http\Message\Response
*/
public function getGuzzleResponse()
{
return $this->guzzleResponse;
}
}
Then you can access the response object as desired
$client = new Your\Name\Space\Client();
$crawler = $client->request('GET', 'http://localhost/redirect');
$response = $client->getGuzzleResponse();
echo $response->getEffectiveUrl();

Categories