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);
Related
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.
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]);
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 !
I'm using API from RapidAPI face verification https://rapidapi.com/HiBrainy/api/face-recognition4 and
I have difficulty using the API
this example code PHP from RapidAPI
$client = new http\Client;
$request = new http\Client\Request;
$body = new http\Message\Body;
$body->addForm(array(
'photo1' => array(
'value' => 'image2.jpg',
'data' => 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAQ=='
),
'photo2' => array(
'value' => 'image2.jpg',
'data' => 'data:image/jpeg;base64,/9j/4AAQSkZ630QAMXaf//Z'
)
), NULL);
$request->setRequestUrl('https://face-recognition4.p.rapidapi.com/FaceVerification');
$request->setRequestMethod('POST');
$request->setBody($body);
$request->setHeaders(array(
'x-rapidapi-host' => 'face-recognition4.p.rapidapi.com',
'x-rapidapi-key' => $my_api_key,
'content-type' => 'multipart/form-data'
));
$client->enqueue($request)->send();
$response = $client->getResponse();
echo $response->getBody();
I applied to laravel with the guzzle package
my code
try {
...other code...
$client = new Client();
$response = $client->post('https://face-recognition4.p.rapidapi.com/FaceVerification', [
'headers' => [
'x-rapidapi-host' => 'face-recognition4.p.rapidapi.com',
'x-rapidapi-key' => $my_api_key,
'content-type' => 'multipart/form-data'
],
'multipart' => [
[
'name' => 'photo1',
'contents' => $image1,
'filename' => 'image1.jpg'
],
[
'name' => 'photo2',
'contents' => $image2,
'filename' => 'image2.jpg'
]
]
]);
}catch (\Exception $error){
dd($error);
}
I got error
#message: """
Client error: `POST https://face-recognition4.p.rapidapi.com/FaceVerification` resulted in a `400 Bad Request` response:
{"type":"https://tools.ietf.org/html/rfc7231#section-6.5.1","title":"One or more validation errors occurred.","status":4 (truncated...)
You should consider removing your API key from the code you're sharing
Try to have a look at the raw request to have further details and verify that your request have been formatted correctly
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));