Symfony 4 - URI must be a string or UriInterface - Guzzle - php

When I try to make a request by PHP Guzzle 7 I am getting the ([2021-10-05 15:50:44] request.CRITICAL: Uncaught PHP Exception InvalidArgumentException: "A 'contents' key is required" at /home/allen/Documents/pim/pim-community-standard/vendor/guzzlehttp/psr7/src/MultipartStream.php line 86 {"exception":"[object] (InvalidArgumentException(code: 0): A 'contents' key is required at /home/allen/Documents/pim/pim-community-standard/vendor/guzzlehttp/psr7/src/MultipartStream.php:86)"} [])
I am trying you upload a picture in Akeneo Pim 5, the endpoint documentation is:
Akeneo Pim 5 - API Documentation
Someone know what I am doing wrong?
Image 01 - Postman
Image 02 - Postman
This request on postman works fine, but I am trying to do it with a guzzle, I already have the first request getting the token.
My Request:
$client2 = new Client();
$file = '/tmp/T_square.jpg';
$url2 = "http://akeneo-pim.local/api/rest/v1/media-files";
$response2 = $client2->request('POST', $url2, [
'headers' => [
'Authorization' => 'Bearer ' . $response->access_token,
'Content-Type' => 'multipart/form-data',
'Accept' => 'application/x-www-form-urlencoded'
],
'multipart' => [
'product' => [
"identifier" => "teste_image",
"attribute" => "picture",
"locale" => null,
"scope" => null
],
'file' => fopen($file, 'r'),
]
]);

Related

param is missing or the value is empty laravel

I am creating a user with the api provided. I am using Laravel and trying to store data to smartrmail and docs to create new subscriber is here https://docs.smartrmail.com/en/articles/636615-list-subscribers
Each time i send request i get following error:
Server error: `POST https://go.smartrmail.com/api/v1/lists/1sptso/list_subscribers` resulted in a `500 Internal Server Error` response: {"error":"param is missing or the value is empty: subscribers"}
{"error":"param is missing or the value is empty: subscribers"}
I am using Laravel and my code is here
Route::get('smartrmail',function(){
$headers = [
'Accept' => 'application/json',
'Authorization' => 'token f91715d5-3aac-4db3-a133-4b3a9493a9a4',
'Content-Type' => 'application/json',
];
$client = new GuzzleHttp\Client([
'headers' => $headers
]);
$data = [
"subscribers"=>[
[
"email"=> "vanhalen#example.com",
"first_name"=> "van",
"last_name"=> "halen",
"subscribed"=> true,
]
]
];
$res = $client->request('POST', 'https://go.smartrmail.com/api/v1/lists/1sptso/list_subscribers', [
'form_params' => [
$data
]
]);
return($res);
// echo $res->getStatusCode();
});
Anybody help me to figure out what is wrong here. I am following this docs
https://docs.smartrmail.com/en/articles/636615-list-subscribers
to create a new subscriber
Instead of
'form_params' => [
$data
]
use
'json' => $data
Explanation
You want to send json data (I assume that because you set header 'Content-Type' => 'application/json', which means that you are sending json), but form_params is used for application/x-www-form-urlencoded.
json sets header to application/json and sends data as json.
As you set proper header, this should work too:
'body' => $data
Proper name of param you can find in Guzzle docs, I used uploading data part.

How to fix the errors in quickbooks API, GuzzleHttlp saying Request has invalid or Unsupported Property

I was working on the Quickbooks API for one of my projects. I'm getting the following error:
GuzzleHttp\Exception\ClientException Client error: POST https://sandbox-quickbooks.api.intuit.com//*Private Info */ resulted in a 400 Bad Request response: {"Fault":{"Error":[{"Message":"Request has invalid or unsupported property","Detail":"Property Name:Unrecognized field \ (truncated...)
The Code:
I have no idea what it is saying to me to fix it. Can anyone help me with this?
Please find the updated code
if(!$buyer->details->quickbooks_id){
$customer = $http->post(
// Sandbox API hidden for security reason
[
'headers' => [
'Accept'=> 'application/json',
'Content-type'=> 'application/json',
'Authorization'=> 'Bearer '.$oauth['access_token']
],
'body' => json_encode(
[
'PrimaryEmailAddr' => [
'Address' => $buyer->email
],
'DisplayName' => $buyer->first_name.' '.$buyer->last_name.' - '.$buyer->details->business_name,
'PrimaryPhone'=>[
'FreeFormNumber'=> $buyer->phone
],
'CompanyName'=>$buyer->details->business_name,
'GivenName'=>$buyer->first_name,
'FamilyName'=>$buyer->last_name,
'BillAddr'=> [
"CountrySubDivisionCode" => $buyer->details->state,
"City" => $buyer->details->city,
"PostalCode" => $buyer->details->zip,
"Line1" => $buyer->details->address,
"Country" => $buyer->details->country
]
])
]
);
$customer = json_decode((string)$customer->getBody(), true);
$buyer->details->quickbooks_id = $customer['Customer']['Id'];
$buyer->details->save();
}
dd($buyer, $buyer->details, $buyer->details->quickbooks_id);
Probably the issue is with the request body. I had the same issue with the data i pass.

GitLab API `400 Bad Request` response: Bad Request

I am working on GitLab API to commit an mp3 file and it was working for years without having problems and the last few weeks it is throwing errors
GuzzleHttp\Exception\ClientException: Client error: `POST https://gitlab.com/api/v4/projects/.../repository/commits/` resulted in a `400 Bad Request` response in file vendor/guzzlehttp/guzzle/src/Exception/RequestException.php on line 113
Here is the code I am using,
$client = new Client([
// Base URI is used with relative requests
'base_uri' => 'https://gitlab.com/',
// You can set any number of default request options.
'timeout' => 300,
]);
$files = [
[
'name' => 'branch',
'contents' => 'TEST'
],
[
'name' => 'commit_message',
'contents' => 'Some message'
],
[
'name' => 'actions[][action]',
'contents' => 'update'
],
[
'name' => 'actions[][file_path]',
'contents' => 'gitlab filepath/test.mp3'
],
[
'name' => 'actions[][content]',
'contents' => file_get_contents('localfilepath/test.mp3')
],
];
$response = $client->request('POST', '/api/v4/projects/.../repository/commits/', [
'headers' => [
'PRIVATE-TOKEN' => $TOKEN
],
'multipart' => $files
]);
return $response->getBody()->getContents();
```
Thanks in Advance
create-new-file-in-repository
POST /projects/:id/repository/files/:file_path
curl --request POST --header 'PRIVATE-TOKEN: <your_access_token>' \
--header "Content-Type: application/json" \
--data '{"branch": "master", "author_email": "author#example.com", "author_name": "Firstname Lastname", \
"content": "some content", "commit_message": "create a new file"}' \
"https://gitlab.example.com/api/v4/projects/13083/repository/files/app%2Fproject%2Erb"
Parameters:
file_path (required) - URL encoded full path to new file. Ex. lib%2Fclass%2Erb
branch (required) - Name of the branch
start_branch (optional) - Name of the branch to start the new commit from
encoding (optional) - Change encoding to base64. Default is text.
author_email (optional) - Specify the commit author’s email address
author_name (optional) - Specify the commit author’s name
content (required) - File content
commit_message (required) - Commit message
Although the document shows that the param of "author_email" and "author_name " is optional, but without these two params will report 400 error, plus then works well

How to used aweber api to add new subscriber usign php and aweber OAuth 2.0 Examples

i am used aweber api in add subscriber and OAuth 2.0 Examples usign php
require_once('aweber_api/aweber_api.php');
$body = [
'ad_tracking' => 'ebook',
'custom_fields' => [
'apple' => 'fuji',
'pear' => 'bosc'
],
'email' => 'anand#gmail.com',
'ip_address' => '192.168.1.1',
'last_followup_message_number_sent' => 0,
'misc_notes' => 'string',
'name' => 'Anand',
'strict_custom_fields' => 'true',
'tags' => [
'slow',
'fast',
'lightspeed'
]
];
$headers = [
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'User-Agent' => 'AWeber-PHP-code-sample/1.0'
];
$listId='myid';
$accountId='myid';
$url = "https://api.aweber.com/1.0/accounts/{$accountId}/lists/{$listId}/subscribers";
$response = $client->post($url, ['json' => $body, 'headers' => $headers]);
echo $response->getHeader('Location')[0];
Error Code :
Notice: Undefined variable: client in D:\xampp\htdocs\Aweber\index.php on line 30
Fatal error: Uncaught Error: Call to a member function post() on null in D:\xampp\htdocs\Aweber\index.php:30 Stack trace: #0 {main} thrown in D:\xampp\htdocs\Aweber\index.php on line 30
The AWeber examples make use of a third party HTTP client for PHP called Guzzle. You need to set up the client first before you'll be able to use it. You can see an example of this in the code examples on AWeber's GitHub here:
https://github.com/aweber/public-api-examples/blob/ea87b1f504cb97d9081a9ea9c8737ae9fd8838e3/php/manage-subscriber
Note the call to create the client:
// Create a Guzzle client
$client = new GuzzleHttp\Client();
Documentation for Guzzle can be found here:
http://docs.guzzlephp.org/en/stable/
It also looks like you're missing the authorization header. When you make the API call it will fail unless you include your access token in the header, so don't forget that part! You can add it to your existing headers like so:
$headers = [
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'User-Agent' => 'AWeber-PHP-code-sample/1.0',
'Authorization' => 'Bearer ' . $accessToken
];
Where $accessToken is a variable you initialize with your token somewhere.

Shopify API - create webhook (batch request)

I've managed to go through creating webhook using Shopify API, but I can create only one webhook per request. I've already tried to customize the request so it could possibly create a few webhooks at once, but it doesn't seem to work.
I'm using GuzzleHttp\Client for my requests and this is what my working request look like:
$client = new Client();
$response = $client->request(
'POST',
"https://{$store}/admin/webhooks.json",
[
'headers' => [
'X-Shopify-Access-Token' => $access_token,
'X-Shopify-Shop-Domain' => $store
],
'form_params' => [
'webhook' => [
"topic" => "orders/create",
"address" => $appAddress,
"format" => "json"
],
]
]);
But when I try something like this:
$client = new Client();
$response = $client->request(
'POST',
"https://{$store}/admin/webhooks.json",
[
'headers' => [
'X-Shopify-Access-Token' => $access_token,
'X-Shopify-Shop-Domain' => $store
],
'form_params' => [
'webhook' => [
[
"topic" => "orders/create",
"address" => $appAddress,
"format" => "json"
],
[
"topic" => "orders/delete",
"address" => $appAddress,
"format" => "json"
]
]
]
]);
Im getting this:
POST https://smshopify.myshopify.com/admin/webhooks.json resulted in
a 422 Unprocessable Entity response: {"errors":{"topic":["can't be blank","Invalid topic specified. Topics allowed: app/uninstalled,
carts/create, carts/u (truncated...)
Is there a way to create couple webhooks in one request, I couldn't find a word about it in Shopify documentation, and my attempts to modify request body are not very successful. What I've managed to do is just foreach topics array and to the single request for every webhook.
No, there is no way to create a batch of webhooks in one request. This is true for most Shopify resources - e.g. products must also be created one-by-one.

Categories