Date and status submit null values via curl in PHP - 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",
];

Related

Passes use multi object array curl php

may I know is the correct way, to pass curl PHP
$data6 = array (
"CorrelationId" => 'CorrelationId',
"ConfirmationId" => 'ConfirmationId',
"Contact.Title" => 'Miss',
"Contact.FirstName" => 'FirstName',
"Contact.LastName" => 'LastName',
"Contact.MobilePhone" => '1234567',
"Contact.HomePhone" => '12356778',
"Contact.Email" => 'admin#gmail.com',
"Contact.Remark" => '',
"Guests[0]Index" => 1,
"Guests[0]Title" => 'Miss',
"Guests[0]FirstName" => 'FirstName',
"Guests[0]LastName" => 'LastName',
"Guests[0]MobilePhone" => '123456',
"Guests[0]HomePhone" => '+123456677',
"Guests[0]Email" => 'admin#gmail.com',
"Guests[0]Type" => 1,
"Guests[0]Age" => 21,
"Beds" => '',);
Below is the original JSON passes data, the original JSON data is from the postman, I running postman is good to go.
I think my mistake was from my code to pass parameter.
{
"CorrelationId": "CorrelationId",
"ConfirmationId": "ConfirmationId",
"Contact": {
"Title": "Miss",
"FirstName": "FirstName",
"LastName": "LastName",
"MobilePhone": "1234567",
"HomePhone": "+6287717564805",
"Email": "admin#gmail.com",
"Remark": ""
},
"Guests": [
{
"Index": 1,
"Title": "Miss",
"FirstName": "FirstName",
"LastName": "LastName",
"MobilePhone": "1234567",
"HomePhone": "+6287717564805",
"Email": "admin#gmail.com",
"Type": 1,
"Age": 21
}
],
"Beds": []
}
Yes marliah, you can pass your array as follows. I suggest there's no need to add an extra brackets to guests. If you do, you have to use index 0 to access data. like Guests[0][Age]
<?php
$data = array(
"CorrelationId" => "CorrelationId",
"ConfirmationId" => "ConfirmationId",
"Contact" => [
"Title" => "Miss",
"FirstName" => "FirstName",
"LastName" => "LastName",
"MobilePhone" => "1234567",
"HomePhone" => "+6287717564805",
"Email" => "admin#gmail.com",
"Remark" => ""
],
"Guests" => [[
"Index" => 1,
"Title" => "Miss",
"FirstName" => "FirstName",
"LastName" => "LastName",
"MobilePhone" => "1234567",
"HomePhone" => "+6287717564805",
"Email" => "admin#gmail.com",
"Type" => 1,
"Age" => 21
]],
"Beds" => []
);
print_r(json_encode($data));
You can use the following function to pass your array to the backend.
Don't forget to use json_encode().
call('POST','API_URL',json_encode($data),'USERNAME','PASSWORD');
If you don't have any authentication setup at the backend, ignore the $username and the $password.
remove this line to ignore authentication credentials.
curl_setopt($curl, CURLOPT_USERPWD, $username . ":" . $password);
.
public function call($method, $url, $data, $username, $password)
{
$curl = curl_init();
switch ($method) {
case "POST":
curl_setopt($curl, CURLOPT_POST, 1);
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
case "PUT":
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
default:
if ($data)
$url = sprintf("%s?%s", $url, http_build_query($data));
}
// OPTIONS:
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
"Content-Type: application/json",
"Accept: application/json",
));
curl_setopt($curl, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
//Check for errors.
if (curl_errno($curl)) {
//If an error occured, throw an Exception.
throw new Exception(curl_error($curl));
}
// EXECUTE:
$result = curl_exec($curl);
if (!$result) {
die("Connection Failures");
}
$httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
echo 'HTTP status: ' . $httpcode;
return $result;
}

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)]
);

Php Magento Api Rest Create Customer Password Issue :

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"
}

php how to send this data threw curl

I have to send this data threw curl:
-d '{"payer": {
"default_payment_instrument":"BANK_ACCOUNT",
"allowed_payment_instruments":["BANK_ACCOUNT"],
"default_swift":"FIOBCZPP",
"contact":{"first_name":"First",
"last_name":"Last",
"email":"first.last#example.com"
}
},
}'
How am I supposed to save those data into fields variable?
$fields = {
"payer": {
"default_payment_instrument":"BANK_ACCOUNT",
"allowed_payment_instruments":["BANK_ACCOUNT"],
"default_swift":"FIOBCZPP",
"contact":{"first_name":"First",
"last_name":"Last",
"email":"first.last#example.com"
}
},
};
$field_string = http_build_query($fields);
curl_setopt($process, CURLOPT_POSTFIELDS, $field_string);
This is GoPay right?
Do something like this:
$fields = [
"payer" => [
"default_payment_instrument" => "BANK_ACCOUNT",
"allowed_payment_instruments" => ["BANK_ACCOUNT"],
"default_swift" => "FIOBCZPP",
"contact" => [
"first_name" => "First",
"last_name" => "Last",
"email" => "first.last#example.com"
]
]
];
$json = json_encode($fields);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
Ok, posting stuff with cURL. Here you go...
<?php
$target_url = "http://domain.dev/post-acceptor.php";
$data_to_post = array(
"payer" => array(
"default_payment_instrument" => "BANK_ACCOUNT",
"allowed_payment_instruments" => "BANK_ACCOUNT",
"default_swift" => "FIOBCZPP",
"contact" => array(
"first_name" => "First",
"last_name" => "Last",
"email" => "first.last#example.com"
)
)
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $target_url);
curl_setopt($curl, CURLOPT_POST, count($data_to_post));
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data_to_post));
$result = curl_exec($curl);
curl_close($curl);
Notes:
you might try to turn your JSON into an PHP array by using json_decode()

PHP Curl - How to do POST request and the parameters should be in JSON format in the body

I need to replicate the same POST request in PHP curl. The request parameters should be in json in the body and the response too is a json object.
{
"api_key": "scTrCT",
"test": "true",
"service_provider_list": [
{
"facility_name": "ALL YOUR SMILE NEEDS DENTAL CENTERS",
"provider_name": "DRS. HERMAN AND MACK P.C",
"tax_id": "12345678
}
],
"payer_ids": [
"00431"
],
"transaction_type": "270",
"effective_date": "2014-01-12"
}
Please try this:
<?php
$json = array(
"api_key" => "scTrCT",
"test" => "true",
"service_provider_list" => array(
"facility_name" => "ALL YOUR SMILE NEEDS DENTAL CENTERS",
"provider_name" => "DRS. HERMAN AND MACK P.C",
"tax_id" => "12345678"
),
"payer_ids" => array(
"00431"
),
"transaction_type" => "270",
"effective_date" => "2014-01-12"
);
$json = json_encode($json);
$ch = curl_init('http://gds.eligibleapi.com/v1.3/enrollment.json');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
echo $result;

Categories