I have started to use GuzzleHttp\Client and I have problem to get response from REST API, because I am getting error 404 code. I don't understand it because when I copied url from my code and paste into browser I will see json response. Do you have any idea what I am doing wrong?
$client = new GuzzleHttp\Client();
$response = $client->get('http://login:password#url_to_api/get/some_products');
$json = $response->json();
I don't think, that you can add your login credentials like that.
Here is the right way: http://guzzle.readthedocs.org/en/latest/clients.html#auth
Related
By using Symfony Panther, I sent a request and I wanted to get the response. I was able to get the body and the status code but for the header I just got an empty array.
$client = Client::createChromeClient();
$client->request('GET', 'https://google.com');
$client->getInternalResponse()->getHeaders(); // returned empty array!!!
Panther does not have access to the HTTP response as explained in this github issue https://github.com/symfony/panther/issues/17.
But if you read carefuly, you'll see that this is not a limitation of Panther but a limitation of Selenium WebDriver. See this post How to get HTTP Response Code using Selenium WebDriver.
This means that the answer to the question "Can I have access to the HTTP response code or the HTTP header using Symfony Panther?" is "No, it's not possible".
While this is not possible the workaround I found was to create an HttpClient and use it to make a request and get the response from it.
<?php
use Symfony\Component\HttpClient\HttpClient;
$client = HttpClient::create();
$response = $client->request('GET', $this->myBaseUri);
$statusCode = $response->getStatusCode();
$headers = $response->getHeaders();
Here's the documentation for HTTP Client (Symfony Docs) if you want to try this way.
According to this issue: https://github.com/symfony/panther/issues/67, it seems that status code is not managed ( HTTP 200 will always get returned, no matter what the request actually responded.)
And same for the headers, I'm afraid. If you look at class
Symfony\Component\Panther\Client and method get($url) you can see that:
$this->internalResponse = new Response($this->webDriver->getPageSource());
while Response's constructor accepts:
public function __construct(string $content = '', int $status = 200, array $headers = [])
Having these said, no matter what happens, you always get HTTP 200 and empty header array.
i'm building an app using Laravel for providing some API for an android application, and i'm using some external APIs from another server (with another URL). i want to make something like proxy or tunnel for external API requests from android side but in my own URL.
for example:
android wants to request for externalUrl.com/api/objects but i want he request to this myDOmain.com/api/x/objects and get the exact same response that the first link returns, without any change.
and there is more than one external API, and i don't want to write separate code for each one.
need something like this :
Route::any('/x/{somewhere}', function($request){
return [$request, externalUrl.com/api/{somewhere}]->response;
})
i'm not asking for http request libraries! i want to redirect request to another domain and return its request.
Best option is to install Guzzle. https://github.com/guzzle/guzzle
It's really simple to use it.
$client = new \GuzzleHttp\Client();
$response = $client->request('GET', 'externalUrl.com/api/{somewhere}');
return $response->getBody();
You can use Guzzle :
use Guzzle\Http\Client;
use Guzzle\Stream\PhpStreamRequestFactory;
[...]
$request = new Client("externalUrl.com/api/{$somewhere}");
$response = $request->send();
return $response->getBody();
I've been trying to post using guzzle but i keep getting the following error : Client error: 404 in console.
The function looks like this:
public function testApi_postRule()
{
$client = new Client(['base_uri' => 'http://10.0.0.0/rule']);
$response = $client->post('placement=guzzle_unique_placement&device=desktop&active=1');
}
So I tried to use postman to see if the problem is in the code so in postman I set the url : http://10.0.0.0/rule and passed the same params with same values and I got the lovely 200 response..
Any idea what am i doing wrong in guzzle?
cURL works fine:
curl -H "X-ApiToken: myapitoken" https://api.fulcrumapp.com/api/v2/records
Guzzle does not:
$client = new Client();
$request = $client->createRequest('GET', "https://api.fulcrumapp.com/api/v2/records");
$request->setHeader("X-ApiToken:" , "myapitoken");
$response = $client->send($request);
This responds with a 401 error: not authorized. This is my first time using Guzzle but in my searches I haven't seen this error. Seems like a simple request so I'm not sure why it is failing.
Thank you!
I had to add another header to explicitly tell it to handle json
$request->setHeader("Accept" , "application/json");
Thank you for pointing out that the 401 was a false error - the real error was the 406, which made me read how to actually fix that.
The setHeader method takes the header name without the colon. Change "X-ApiToken:" to "X-ApiToken".
I'm trying to consume the Stack Exchange API with Guzzle. I am facing an issue where I can't get the JSON response back: it apparently fails when parsing it.
Here is my code:
$client = new GuzzleHttp\Client();
$parameters = ['pagesize'=>'2','order'=>'desc','sort'=> 'activity','q'=>'laravel eloquent','site'=>'stackoverflow'];
$response = $client->get('http://api.stackexchange.com/2.2/search/advanced',['query' => $parameters ]);
The resultant effective URL that Guzzle creates is correct: if you open the link in your browser you'll see that it works fine and returns the requested data.
However, Guzzle fails with this error when trying to access the JSON with $response->json():
GuzzleHttp \ Exception \ ParseException
Unable to parse JSON data: JSON_ERROR_UTF8 - Malformed UTF-8 characters, possibly incorrectly encoded
After reading the documentation again, I believe that the request is compressed and I am not passing the appropriate content header. If this is so, can you please let me know which header I should be passing to get the correct response?
Ok so the following code works for me.
$client = new GuzzleHttp\Client();
$parameters = ['pagesize'=>'2','order'=>'desc','sort'=> 'activity','q'=>'laravel eloquent','site'=>'stackoverflow'];
$params = http_build_query($parameters);
$request = $client->createRequest('GET', 'http://api.stackexchange.com/2.2/search/advanced?'.$params);
$request->addHeader('Accept-Encoding','GZIP');
$request->addHeader('Content-Type','application/json');
$response = $client->send($request);
var_dump($response->json());