Php Magento Api Rest Create Customer Password Issue : - php

I'm using the Magento ver. 2.1.2 Rest Api to create users, following this :
http://devdocs.magento.com/guides/m1x/api/rest/Resources/resource_customers.html#RESTAPI-Resource-Customers-HTTPMethod-POST-customers
$data = [
"customer" => [
"firstname" => 'Earl',
"lastname" => 'Hickey',
"email" => 'earl-2#example.com',
"password" => 'password',
"website_id" => 1,
'store_id' => 1,
"group_id" => 1
]
];
$token = $this->get('lp_api')->getToken();
$ch = curl_init( $this->endpoint . 'customers');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: application/json", "Authorization: Bearer " . json_decode( $token ),
)
);
// var_dump(curl_getinfo($c));
$result = curl_exec($ch);
If i send a password (as in the example above), i've got the following error :
Next Exception: Report ID: webapi-583357a3bf02f; Message: Property "Password" does not have corresponding setter in class "Magento\Customer\Api\Data\CustomerInterface". in /var/www/html/www.magento.dev/vendor/magento/framework/Webapi/ErrorProcessor.php:195
I noticed that if i remove the "password" => 'password' from the $data array, a user is created without password (seems odd to me).
I can't find any help on this error.
Any idea anyone ?

Refer below link for Magento 2.x version.
http://devdocs.magento.com/swagger/index_20.html#/
I have used below body for creating customers through Rest Api and it worked properly.
{
"customer": {
"email": "xyz#abc.com",
"firstname": "x",
"lastname": "y",
"website_id":1,
"group_id":1,
"custom_attributes": [
{
"attribute_code": "mobile_no",
"value": "1234567890"
}
]
},
"password": "123456"
}

Related

Freshdesk API: Attach dataurl as file using PHP and CURL

I'm trying to create a ticket in Freshdesk with a screenshot as an attachment. Screenshot is captured in a canvas and converted to dataurl. And I'm using the following code to pass it to the Freshdesk API (V2)
$data = [
"description" => $messageBody,
"subject" => 'Bug Report from Client',
"email" => $replyTo,
"priority" => 1,
"status" => 2,
"attachments" => [
[
"type" => "file",
"name" => "Screenshot",
"content-type" => "image/png",
"resource" => $dataUrl,
]
]
];
$url = "https://domain.freshdesk.com/api/v2/tickets";
$apiKey = "key";
$headers = array(
'Content-Type:application/json',
);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $apiKey);
$resultStr = curl_exec($ch);
It results in the following error.
{
field: "attachments",
message: "It should contain elements of type valid file format only",
code: "datatype_mismatch"
}
Without the attachments field, the above code works fine. How do I fix this?

Date and status submit null values via curl in PHP

I have a following request method below which I am posting to an API via Curl
{
"status": [
{
"status": "string",
"date": "string"
}
],
"first_name": "string",
"last_name": "string"
}
The first_name and last_name values get posted successfully but status and date parameters submitted empty values. How do I also get the value of status and date parameters to be submitted also?
Here is the code:
<?php
$tok ='my token goes here';
$params= array(
'first_name' => "nancy",
'last_name' => "moree",
'status' => 'active',
'date' => '2020-12-29'
);
$url ='https://app.drchrono.com/api';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: Bearer $tok"));
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$output = curl_exec($ch);
echo $output;
As described by the JSON requirement:
"status": [
{
"status": "string",
"date": "string"
}
]
Your actual status need to be an object inside another array key status:
$params= array(
'first_name' => "nancy",
'last_name' => "moree",
'status' => array(
array('status' => 'active', 'date' => '2020-12-29')
)
);
The array you're POSTing does not match the format you said you wanted to use. Try:
$params = [
'status' => [
'status': "active",
"date": "2020-12-29"
],
'first_name' => "nancy",
'last_name' => "moree",
];

How to format POST request using PHP curl methods?

I'm trying to send a post request with this payload:
$request_content = [
"data" => [
[
"sku" => "0987",
"price" => $price,
"category" => "moveis",
"brand" => "bartira",
"zip_code" => "07400000",
"affiliate" => "google-shopping"
]
]
];
Since it's a post i set the CURLOPT_POST to true;
$encoded_request = json_encode($request_content);
$ch = curl_init("https://my-service/endpoint/");
curl_setopt($ch, CURLOPT_HTTPHEADER, ["Authorization: Token my-token"]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $encoded_request);
The $encoded_request content shown in print_r is:
{
"data": [
{
"sku": "0987",
"price": "5.99",
"category": "moveis",
"brand": "bartira",
"zip_code": "07400000",
"affiliate": "google-shopping"
}
]
}
If i use this content on the Postman i get the right response from the service that i'm requesting, but on my code i got the error;
{"data":["This field is required."]}
Which configuration i'm missing on curl_ to format the payload correctly?
You can try to set CURLOPT_HTTPHEADER and change your variable $request_content, something like this:
//set your data
$request_content = [
"data" => [
"sku" => "0987",
"price" => $price,
"category" => "moveis",
"brand" => "bartira",
"zip_code" => "07400000",
"affiliate" => "google-shopping"
]
];
$encoded_request = json_encode($request_content);
$ch = curl_init("https://my-service/endpoint/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $encoded_request);
// Set HTTP Header for POST request
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Token my-token',
'Content-Type: application/json',
'Content-Length: ' . strlen($encoded_request)]
);

Stripe API create SKU

I need to create a SKU via stripe API.
The problem is in inventory field.
Stripe api response is:
'error' => [
'message' => 'Invalid hash',
'param' => 'inventory',
'type' => 'invalid_request_error'
]
My php code is:
$endPoint = 'https://api.stripe.com/v1/skus';
$APIKEY_TEST = 'my_api_key';
$headers = array('Authorization: Bearer '.$APIKEY_TEST);
$sku = [
'active' => 'true',
'inventory' => ['quantity' => 10000000 ,'type' => 'infinite', 'value' => null],
"currency" => "eur",
"price" => $price,
"product" => $stripe_product_id
];
$array_string ='';
foreach($sku as $key => $value) {
$array_string .= $key.'='.$value.'&';
}
rtrim($array_string, '&');
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $endPoint);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $array_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$output = curl_exec($ch);
curl_close($ch);
In stripe api docs inventory is hash type field.
I have tried json_encode() without luck.
Maybe the problem is in sending an array instead of a hash.
In $sku array, inventory field is also an nested associative array.
Maybe the problem resides there as well.
Is there a way to send CURLOPT_POSTFIELDS containing inventory so that stripe accepts it?
EDIT:
In Stripe dashboard i can see my request:
{
"active": "true",
"inventory": "Array",
"currency": "eur",
"price": "3",
"product": "prod_F6ipvfYFvOxxQq"
}
Inventory field has no data, but instead "Array".
After trying a lot of possible solutions, i found the answer:
$post_array = http_build_query($sku);
And know stripe accepts the $sku array with nested inventory array.
It worth notice that stripe does not accept JSON in requests.
The request has to be url encoded.

Google QPX Express API PHP

I'm trying to use QPX Express API for my website to search for flights.
https://developers.google.com/qpx-express/v1/requests#Examples
I have no idea how to run
curl -d #request.json --header "Content-Type: application/json" https://www.googleapis.com/qpxExpress/v1/trips/search?key=xxxxxxxxxxxxxx
from my php file. And how should I manage the json file. I quess I should make a php file and set the header type, am I correct?
I could not find anything from anywhere
You do not need to create and save an actual JSON file for each request. You can simply create a JSON string and send that as the POST payload. As far as executing the curl, you should see the native functions available in the PHP Manual. Specifically, curl_init(), curl_setopt(), and curl_exec(). Here's an example...
$url = "https://www.googleapis.com/qpxExpress/v1/trips/search?key=YOUR_API_KEY_HERE";
$postData = '{
"request": {
"passengers": {
"adultCount": 1
},
"slice": [
{
"origin": "BOS",
"destination": "LAX",
"date": "2016-05-10"
},
{
"origin": "LAX",
"destination": "BOS",
"date": "2016-05-15"
}
]
}
}';
$curlConnection = curl_init();
curl_setopt($curlConnection, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
curl_setopt($curlConnection, CURLOPT_URL, $url);
curl_setopt($curlConnection, CURLOPT_POST, TRUE);
curl_setopt($curlConnection, CURLOPT_POSTFIELDS, $postData);
curl_setopt($curlConnection, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($curlConnection, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlConnection, CURLOPT_SSL_VERIFYPEER, FALSE);
$results = curl_exec($curlConnection);
You could also use an array to create the payload, and then use json_encode() to convert it into a JSON string.
$postData = array(
"request" => array(
"passengers" => array(
"adultCount" => 1
),
"slice" => array(
array(
"origin" => "BOS",
"destination" => "LAX",
"date" => "2016-05-10"
),
array(
"origin" => "LAX",
"destination" => "BOS",
"date" => "2016-05-15"
)
)
)
);
And then use
curl_setopt($curlConnection, CURLOPT_POSTFIELDS, json_encode($postData));

Categories