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 !
Related
i try to hit an API with Laravel via Guzzle but i keep get the same error :
GuzzleHttp\Exception\ConnectException: cURL error 35: error:0A000172:SSL routines::wrong signature type
I have tried all the solution in the internet like set verify to false :
$client = new Client([
'headers' => [ 'Content-Type' => 'application/x-www-form-urlencoded' ],
'verify' => false
]);
or setting down the SSL version :
$client = new Client([
'headers' => [ 'Content-Type' => 'application/x-www-form-urlencoded' ],
'curl' => array(
CURLOPT_SSLVERSION => CURL_SSLVERSION_TLSv1_2
)
]);
but i have no luck. I keep get the same error message.
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?
I am trying to create an order in paypal with laravel and guzzle and it throws me this error:
GuzzleHttp\Exception\ClientException Client error: POST https://api-m.sandbox.paypal.com/v2/checkout/orders resulted in a
400 Bad Request response:
{"name":"INVALID_REQUEST","message":"Request is not well-formed,
syntactically incorrect, or violates schema.","debug_id (truncated...)
my controller code:
$accessToken = $this->getAccessToken(); $client = new Client(['base_uri' => 'https://api-m.sandbox.paypal.com/v2/checkout/']);
$headers = [
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . $accessToken,
];
$params = [
'intent' => 'CAPTURE',
'purchase_units' => [
'amount' => [
'currency_code' => 'USD',
'value' => '100.00'
]
]
];
//dd($params);
$response = $client->request('POST', 'orders', [
'headers' => $headers,
'form_params' => $params
]);
Your $params object is invalid. It should be a list of purchase_unit items, not an associative array.
$params = [
'intent' => 'CAPTURE',
'purchase_units' => [
[
'amount' => [
'currency_code' => 'USD',
'value' => '100.00'
]
]
]
];
https://developer.paypal.com/api/orders/v2/#orders-create-request-body
'form_params' => $params
This is used to send an application/x-www-form-urlencoded POST request, which the PayPal API does not use.
You should be posting a plain string, JSON encoded. In place of form_params try passing the json key to guzzle in the request, or read its documentation on how to send json
edit: not sure whether that change makes a difference, but the other answer is correct that purchase_units needs to be an indexed array of purchase_unit objects -- likely only one of them.
My situation is
using Laravel 7
want to use Shopee API
Sending a request in Laravel should be like this
$res = Http::withHeaders([
'Content-Type' => 'application/json',
'Authorization' => $secret_key
])->post($api_url, [
"ordersn_list" => [$order_no],
"shopid" => $shop_id,
"partner_id" => $partner_id,
"timestamp" => $timestamp
]);
But Shopee API needs no space in the body part (cannot send as JSON format). I have tried
$res = Http::withHeaders([
'Content-Type' => 'application/json',
'Authorization' => $secret_key
])->post($api_url, $body_string);
It does not work because it must be an array. return error Argument 2 passed to Illuminate\Http\Client\PendingRequest::post() must be of the type array, string given
.
Try this:
$data = [
"ordersn_list" => [$order_no],
"shopid" => $shop_id,
"partner_id" => $partner_id,
"timestamp" => $timestamp
];
$res = Http
::asJson()
->withHeaders([
'Authorization' => $secret_key
])
->post($api_url, $data);
Or:
$data = [
"ordersn_list" => [$order_no],
"shopid" => $shop_id,
"partner_id" => $partner_id,
"timestamp" => $timestamp
];
$res = Http
::withHeaders([
'Authorization' => $secret_key
])
->withBody(json_encode($data), 'application/json')
->post($api_url);
I need to integrate an API so I write function:
public function test() {
$client = new GuzzleHttp\Client();
try {
$res = $client->post('http://example.co.uk/auth/token', [
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded',
],
'json' => [
'cliend_id' => 'SOMEID',
'client_secret' => '9999jjjj67Y0LBLq8CbftgfdreehYEI=',
'grant_type' => 'client_credentials'
]
]);
$res = json_decode($res->getBody()->getContents(), true);
dd($res);
}
catch (GuzzleHttp\Exception\ClientException $e) {
$response = $e->getResponse();
$result = json_decode($response->getBody()->getContents());
return response()->json(['data' => $result]);
}
}
as a responde I got message:
{"data":{"error":"invalid_clientId","error_description":"ClientId should be sent."}}
Now when I try to run the same url with same data in POSTMAN app then I get correct results:
What is bad in my code? I send correct form_params also I try to change form_params to json but again I got the same error...
How to solve my problem?
The problem is that in Postman you're sending the data as a form, but in Guzzle you're passing the data in the 'json' key of the options array.
I bet that if you would switch the 'json' to 'form_params' you would get the result you're looking for.
$res = $client->post('http://example.co.uk/auth/token', [
'form_params' => [
'client_id' => 'SOMEID',
'client_secret' => '9999jjjj67Y0LBLq8CbftgfdreehYEI=',
'grant_type' => 'client_credentials'
]
]);
Here's a link to the docs in question: http://docs.guzzlephp.org/en/stable/quickstart.html#sending-form-fields
Also, I noticed a typo - you have cliend_id instead of client_id.