Getting cURL error 61 and no response for discover.search.hereapi.com/v1/discover endpoint - format

We are using https://discover.search.hereapi.com/v1/discover to perform a free-form text query for a latitude, longitude center.
Example: https://discover.search.hereapi.com/v1/discover?q=cafe+in+harrow&at=51.52236%2C-0.13993
The code block in PHP is
`$headers = [
'Authorization' => 'Bearer ' . $token,
'Accept' => 'application/json',
];
$client = new Client();
$response = $client->request($method, $url, [
'headers' => $headers
]);`
Now the problem is sometimes this endpoint do not return response due to cURL error 61
Error while processing content unencoding: incorrect header check (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
When this happen we have to change the code to
`$headers = [
'Authorization' => 'Bearer ' . $token,
'Accept' => 'application/json',
];
$client = new Client();
$response = $client->request($method, $url, [
'headers' => $headers,
'decode_content' => 'gzip'
]);`
After adding 'decode_content' => 'gzip' it works.
But again after some days it stop working and then we have to remove this 'decode_content' => 'gzip' line
Please guide us how to solve this issue?

Related

Make Guzzle POST Request in Laravel using 2 types of authorization

I am trying to make a post request to a url that first requests for a username and password and then I provide the api_key to authorize myself in Laravel but without success. Can you please help?
$credentials = base64_encode('username1' .':' . 'password1');
try {
$defaultValues = [
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'Authorization' => 'Basic '. $credentials,
],
'auth' => 'Bearer ' . $api_key,
'verify' => false
];
$client = new Client($defaultValues);
$response = $client->post(config('myurl') . '/payments' . '/createPayment', ['json' => $data]);
Thanks
Change your code to this:
$credentials = base64_encode('username1' .':' . 'password1');
try {
$defaultValues = [
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'Authorization' => 'Basic '. $credentials,
],
'api_token' => $api_key,
'verify' => false
];
$client = new Client($defaultValues);
$response = $client->post(config('myurl') . '/payments' . '/createPayment', ['json' => $data]);

UnauthorizedError: No authorization token was found with Laravel 8

I'm trying to make a GET request passing a bearer token as authentication.
I try to pass the token with:
$response = Http::get($url, [
'headers' => [
'Authorization' => 'Bearer ' . $token,
'Accept' => 'application/json',
],
]);
as stated in the docs
When I check the value of the variables, I get:
$token : "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2MDc0NTI0NzksInVzZXJuYW1lIjoic3VuY2hhaW4iLCJvcmdOYW1lIjoib3JnMCIsImlhdCI6MTYwNzQxNjQ3OX0.LUTFZh8Em13f3cQ8TpxgayVRC9XvVHyczOhQXARxk48"
$url : "https://example.com/channels/common/chaincodes/main?peer=org0/peer0&args=%5B%222020-12-01T22%3A00%3A00Z%22%2C+%222020-12-01T22%3A30%3A00Z%22%5D&fcn=GetMeasuresBetween"
But when I copy paste those values in Postman, GET is working and I can get my data, which means data is correct, and the way I execute my GET request might be incorrect.
Where am I wrong ? It seems all good to me !
I found the solution,
$response = Http::withToken($token)->get($url, [
'headers' => $headers,
'peer' => $this->peer,
'args' => $arrayArgs,
'fcn' => "GetMeasuresBetween",
]);
Or, you may use guzzle, and initialize
$client = new \GuzzleHttp\Client(['base_uri' => $this->url]);
$response = $client->request('GET', $url, [
'headers' => $headers,
'peer' => $this->peer,
'args' => $arrayArgs,
'fcn' => "GetMeasuresBetween",
]);
I don't know why it is not working the first way, but it is now working !

Using Guzzle in Laravel with POST

I want to send a POST request to an external API
Here is my method to call
public function recursub(Request $request) {
$users = User::where('cancel_trial', 1)->get();
foreach($users as $user) {
//dd($user->email);
$url= 'https://api.paystack.co/subscription';
$client = new Client();
$response = $client->request('POST', $url, [
'headers' => [
'Content-Type' => 'application/json',
'Authorization' => 'Bearer sk_test_0994##############',
],
'form_params' => [
'customer' => '233########',
'plan' => 'PLN_###########',
]
]);
dd($response->getBody());
//dd($response->getBody()->getContents());
}
}
But I keep getting this error when I call the endpoint
GuzzleHttp \ Exception \ ClientException (400)
Client error: POST https://api.paystack.co/subscription resulted in a 400 Bad Request response: { "status": false, "message": "Request body could not be parsed. Make sure request body matches specified content-ty (truncated...)
It's looks like that you send wrong request to API.
If you set application/json maybe you should pass parameter in body, not in form params:
$response = $client->request('POST', $url, [
'headers' => [
'Content-Type' => 'application/json',
'Authorization' => 'Bearer sk_test_0994##############',
],
'body' => json_encode([
'customer' => '233########',
'plan' => 'PLN_###########',
])
]);
Another information that maybe can be helpful is that you can disable exception with ['http_errors' => false] option (http://docs.guzzlephp.org/en/stable/request-options.html#http-errors).

Guzzle authentication returning 400 error

I'm trying to authenticate in an api sandbox but error 400 is returning me, I already got it through an extension but in the code with guzzle it's not working, I need to pass the Header parameter with encrypted authorization as it is.
$client = new \GuzzleHttp\Client(['headers' => ['Authorization' => 'Basic $token']]);
$response = $client->post('', [
'grant_type' => 'password',
'scope' => 'forintegration',
'username' => '',
'password' => '',
]);
$body = $response->getBody();
print_r(json_decode((string) $body));
Working with:
$client = new \GuzzleHttp\Client();
$url = "";
$response = $client->request('POST', $url, [
'headers' => [
'Accept' => 'application/x-www-form-urlencoded',
'Authorization' => 'Basic $token'
],
'form_params' => [
'grant_type' => 'password',
'scope' => 'forintegration',
'username' => '',
'password' => ''
]
]);
$body = $response->getBody();
print_r(json_decode((string) $body));

Guzzle 6.0 download file using request headers

I can't find any example of how to download a remote file using Guzzle 6.0. I need to pass headers in the GET request.
I have looked at the documentation which isn't helpful at all.
I came up with this but it still doesn't download a file
require_once('vendor/autoload.php');
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Client;
$client = new Client();
$response = $client->request('GET', '/stream/20', [
'headers' => [
'Authorization: Token token' => 'df456g4fd564gfs65dg45s6fdg4dsf5g4sd65g',
'Cache-Control' => 'no-cache',
'Content-Type' => 'application/pdf'
],
'sink' => 'https://example.com/path/to/file',
]);
Has anyone successfully downloaded a file using request headers?
I think you got confused.
Where you have '/stream/20' is the url of where the file is you want to download.
The sink part is where you want to save your image.
Try this....
require __DIR__ . '/vendor/autoload.php';
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Client;
$client = new Client();
$resource = fopen('test.pdf', 'w');
$response = $client->request('GET', 'https://example.com/path/to/file', [
'headers' => [
'Authorization' => 'Token token=df456g4fd564gfs65dg45s6fdg4dsf5g4sd65g',
'Cache-Control' => 'no-cache',
'Content-Type' => 'application/pdf'
],
'sink' => $resource,
]);
echo 'downloaded';

Resources