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;
}
Related
I would like to set my "message of absence" in strato webmail programmatically.
For that I open the developer tools (firefox) and I login via strato webmail and set the "message of absence". I get the curl request from the developer console and I rebuild this as a php code:
$myJson = '
{
"actioncmds": [
{
"addresses": [
"mail#domain.de"
],
"days": "7",
"id": "vacation",
"subject": "My Subject",
"text": "My Message"
}
],
"active": true,
"flags": [
"vacation"
],
"id": 5,
"position": 0,
"rulename": "Abwesenheitsbenachrichtigung",
"test": {
"id": "allof",
"tests": [
{
"comparison": "ge",
"datepart": "date",
"datevalue": [
1672272000000
],
"id": "currentdate",
"zone": "+0100"
},
{
"comparison": "le",
"datepart": "date",
"datevalue": [
1672876800000
],
"id": "currentdate",
"zone": "+0100"
}
]
}
}';
function stratoCurl( $url, $type, $parameter ) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $type);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, TRUE);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2TLS);
curl_setopt($ch, CURLOPT_ENCODING, '');
curl_setopt($ch, CURLOPT_POSTFIELDS, $parameter);
curl_setopt($ch, CURLOPT_HTTPHEADER,
array (
'Content-Type: application/x-www-form-urlencoded'
),
);
$response = json_decode(curl_exec($ch), true);
curl_close($ch);
return $response;
}
$stratoLogin = stratoCurl("https://webmail.strato.de/appsuite/api/login", "POST", http_build_query(array (
"action" => "login",
"name" => "myUserName",
"password" => "myPassword"
)));
$setMessage = stratoCurl("https://webmail.strato.de/appsuite/api/mailfilter/v2?action=update&session=".$stratoLogin['session'], "PUT", $myJson);
echo '<pre>';
print_r($stratoLogin);
print_r($setMessage);
echo '</pre>';
Result:
Array
(
[session] => MY_SESSION_ID
[user] => myUsername
[user_id] => 9
[context_id] => 52515441
[locale] => de_DE
)
Array
(
[error] => Your session expired. Please login again.
[error_params] => Array
(
[0] => MY_SESSION_ID
)
[categories] => TRY_AGAIN
[category] => 4
[code] => SES-0203
[error_id] => -673980302-22682517
[error_desc] => Your session MY_SESSION_ID expired. Please start a new browser session.
)
Login seems to be successfull, but if I would like to set the message, the session expired?? Any idea?
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",
];
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"
}
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()
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;