I'm trying to make a request to the Walmart API (Item Associations) to retrieve shippingTemplate and shipNode per items. I'm following this guide.
https://developer.walmart.com/api/us/mp/items#operation/getItemAssociations
I set the required field to CURLOPT_POSTFIELDS and I encountered this error.
Result from Walmart API
This is my code:
`
$url_returns ="https://marketplace.walmartapis.com/v3/items/associations";
$ch_returns = curl_init();
$qos_returns = uniqid();
$fields = ['items' => ['123456','778990'] ]; // note: not the real data.
$options_returns = array(
CURLOPT_URL => $url_returns,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 60,
CURLOPT_HEADER => false,
CURLOPT_HTTPHEADER => array(
"WM_SVC.NAME: Walmart Marketplace",
"WM_QOS.CORRELATION_ID: $qos_returns",
"Authorization: Basic $authorization_key",
"WM_SEC.ACCESS_TOKEN:$token",
"Accept: application/json",
),
CURLOPT_POSTFIELDS => json_encode($fields),
);
curl_setopt_array($ch_returns, $options_returns);
$response_returns = curl_exec($ch_returns);
$code_returns = curl_getinfo($ch_returns, CURLINFO_HTTP_CODE);
curl_close($ch_returns);
$response_returns = json_decode($response_returns,true);`
Am I missing something?
Thank you in advance.
Looks like the list of items whose associations you are trying to fetch are invalid.
Also, could you please double check WM_SVC.NAME header param value
Regards,
Firdos
IOSupport
Related
I am testing an api on postman. The request body should be in x-www-form-url-encoded. My requests are being passed successfully, and am able to generate a snippet which I have shared here. However, some of the parameters that am adding to the body (Amount, and phone number) will not be static when the api is employed on my site. These parameters will vary by user. I have tried to define those parameters at the top of the code as you cane see $Airtime_amount and
$Recieving_mobile, but how can I pass them to the x-www-form-url-encoded CURLOPT_POSTFIELDS in the code below? See how am trying to pass them, but without success...
In other words, I have a url encoded string from postman, but the parameters in that string are static. i would like to make them dynamic..Like get user phone number from wordpress, and insert it in the urlencoded string
//Wordpress hook to call the api begins here
add_action('hrw_withdrawal_request_notification','disburse_airtime',7);
function disburse_airtime() {
$Airtime_amount = "KES 230";
$Recieving_mobile = "+254757777777";
//Snippet generated from postman begins here
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://api.sandbox.africastalking.com/version1/airtime/send',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => 'username=sandbox&recipients=%5B%7B%22phoneNumber%22%3D%3E%24Recieving_mobile%2C%22amount%22%3D%3E%24Airtime_amount%7D%5D',
CURLOPT_HTTPHEADER => array(
'Content-Type: application/x-www-form-urlencoded',
'apiKey: 61449ca078574078a6d0eaaa01cfb751f803797c99714f74d8541a25e2a612ef',
'Accept: application/json'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
}
You should probably build the POSTFIELDS string using the http_build_query function.
Your existing string has what looks like a JSON string for the value of the recipients parameter, so we can build that up using arrays, then encode it when we set it in the params.
function disburse_airtime()
{
$Airtime_amount = "KES 230";
$Recieving_mobile = "+254757777777";
$recipients = [
[
'phoneNumber' => $Recieving_mobile,
'amount' => $Airtime_amount
]
];
$params = [
'username' => 'sandbox',
'recipients' => json_encode($recipients)
];
$postFields = http_build_query($params);
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => 'https://api.sandbox.africastalking.com/version1/airtime/send',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $postFields,
CURLOPT_HTTPHEADER => [
'Content-Type: application/x-www-form-urlencoded',
'apiKey: 61449ca078574078a6d0eaaa01cfb751f803797c99714f74d8541a25e2a612ef',
'Accept: application/json'
],
]);
$response = curl_exec($curl);
curl_close($curl);
echo $response;
}
Side note, if you want to "reverse engineer" the data that is in the encoded string in order to build it up in your own code, you can do so with using urldecode and parse_str:
$str = 'username=sandbox&recipients=%5B%7B%22phoneNumber%22%3D%3E%24Recieving_mobile%2C%22amount%22%3D%3E%24Airtime_amount%7D%5D';
parse_str(urldecode($str), $params);
print_r($params);
Result:
Array
(
[username] => sandbox
[recipients] => [{"phoneNumber"=>$Recieving_mobile,"amount"=>$Airtime_amount}]
)
I got myself some postmates customer and api credentials (https://partner.postmates.com/welcome-api)
followed the example here https://documenter.getpostman.com/view/3967924/RW1Yq1oN#993c52be-abcc-b06b-3f3d-0abfe9782484 like so
$customerId = 'cus_xxxxxxxxxx';
$apiKey = 'aaaaaa-bbbbb-ccc-dddd-eeeeeeee';
$authKey = base64_encode($apiKey.':');
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://api.postmates.com/v1/customers/'.$customerId.'/delivery_quotes',
CURLOPT_HEADER => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => array('pickup_address' => '76 9th Ave, New York, NY','dropoff_address' => '46 Maujer St, Brooklyn, NY'),
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_HTTPHEADER => array(
'Content-Type: application/x-www-form-urlencoded',
'Accept: application/json',
'Authorization: Basic '.$authKey.'',
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
exit();
(of course using the actual customerId and apiKey)
but i only ever get as a result the following:
{"code":"unauthorized","message":"Invalid OAuth 2.0 credentials provided."}
(the CURLOPT_SSL_VERIFYPEER = false is just added for simplicities sake in a test environment here, using it on a live server with ssl verified yields the same results)
I have of course double and triplechecked the customerid and api key, but the result does not change
If anyone has any idea what i am missing , that would be great as i have been banging my head against the wall for hours now
Hi im trying to use a custom form on my website to add a new contact to our marketing list, each contact will contain an email and first name.
Im trying to follow this documentation but am having no success:
https://sendgrid.api-docs.io/v3.0/contacts/add-or-update-a-contact
I have tried using their tool but it always says incorrect JSON but when i use an online validator it says correct, I can't figure out how to match theirs to get my request to post.
This is my current code:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.sendgrid.com/v3/marketing/contacts",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_POSTFIELDS => "{\"list_ids\":[\"bf3ce5bd-14a2-414b-9b81-*****8e8ea62\"],\"contacts\":[{\"email\":\"$email\",\"first_name\":\"$first_name\"]}",
CURLOPT_HTTPHEADER => array(
"authorization: Bearer SG.OCFHb********P3iuikQ.bqKdM-da7X609ZNo9UT7y*********u5fDlfQo80o",
"content-type: application/json"
),
));
You just should be added } chars after $first_name.
This is valid JSON:
CURLOPT_POSTFIELDS => "{\"list_ids\":[\"bf3ce5bd-14a2-414b-9b81-*****8e8ea62\"],\"contacts\":[{\"email\":\"$email\",\"first_name\":\"$first_name\"}]}",
You can check it with https://jsonformatter.curiousconcept.com/ validator website.
One little trick to not have to deal with all the backslashes and escaping is to use a pre-defined object literal in PHP, by using arrays with (object) in front of them, to define the objects nested in the structures.
make an object, turn it into JSON with json_encode
enter the encoded JSON object into the CURLOPT_POSTFIELDS in the cURL request.
<?php
// make custom fields object for entry into the cURL later on:
// THERE IS NO OBJECT LITERAL IN PHP, but using (object) in front of an array, voila
$contact_info = (object)[
"list_ids" => [
"eeee-eeee-eeee-eeee-eeeeexample" // array of list ids here.
],
"contacts" => [ // this is an array of objects (array[object]), according to the api-docs.
(object)[
"email" => "email#example.com",
"country" => "the country",
"city" => "the city",
"custom_fields" => (object)[
"e1_T" => "sign-up-page-name", // only custom fields use ids as the key.
"e2_T" => "next-custom-field" // keep adding custom fields in this array.
]
]
]
];
// now all we have to do is to encode the object into JSON:
$json_contact_data = json_encode($contact_info);
// now add the contact:
$curl = curl_init();
// update the contact
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.sendgrid.com/v3/marketing/contacts",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_POSTFIELDS => $json_contact_data, // here we enter the pre-configured json from above.
CURLOPT_HTTPHEADER => array(
"authorization: Bearer " . $your_api_key . "",
"content-type: application/json"
)
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
$decoded = json_decode($response, true); // decode into associative array.
if ($err) {
echo "cURL Error #: " . $err;
} else {
print_r($decoded); // print the decoded json message.
}
?>
This is the best way:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://api.sendgrid.com/v3/marketing/contacts',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'PUT',
CURLOPT_POSTFIELDS =>'{"list_ids": ["list-ID"], "contacts": [{"email": "' . $_GET["email"] . '"}]}',
CURLOPT_HTTPHEADER => array(
': ',
'Authorization: Bearer SG.000000000',
'Content-Type: application/json'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
die();
So I'm trying to cancel notifications using the REST API of oneSignal. I have successfully sent them and scheduled them using the oneSignal REST API but canceling is proving to be difficult. Mostly it is because in the documentation the cURL is:
curl --include \
--request DELETE \
--header "Authorization: Basic NGEwMGZmMjItY2NkNy0xMWUzLTk5ZDUtMDAwYzI5NDBlNjJj" \
https://onesignal.com/api/v1/notifications/{notificationId}?app_id={appId}
This is my PHP code so far:
$ch = curl_init();
$httpHeader = array(
'Authorization: Basic MY_REST_API_KEY'
);
$url = "https://onesignal.com/api/v1/notifications/" . NOTIFICATION_ID . "?app_id=" . APP_ID;
$options = array (
CURLOPT_URL => $url,
CURLOPT_HTTPHEADER => $httpHeader,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_CUSTOMREQUEST => "DELETE",
);
curl_setopt_array($ch, $options);
$response = curl_exec($ch);
curl_close($ch);
I'm not too sure how to convert the shell to php and right now it is not deleting the notifications. the $response isn't returning anything meaning it is not working. Any help would be awesome.
Thanks
I had to add one more option to my options array so basically this:
$options = array (
CURLOPT_URL => $url,
CURLOPT_HTTPHEADER => $httpHeader,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_CUSTOMREQUEST => "DELETE",
CURLOPT_SSL_VERIFYPEER => FALSE
);
That allowed the request to go through!
Do any of you know a way to get datas from a database of a website? I already studied and made a program that POST data using json. It gets the shipment status of a certain tracking number and if it's shipped, it will be changed to delivered. And I already done that.
Now what I'm trying to do is get all the datas, or the row of that tracking number and then encode it to JSON. But I don't know what to do.
This is how I did the first program: on getting a specific tracking number and update the status to delivered.
<?php
$url='https://sample.com.ph';
$apikey='apikey';
$method ='SendPackageStatus';
$data = array(
'package' => array(
'tracking_number' => '735898532',
'package_status' => 'delivered',
'failed_reason' => '',
'updated_at' => '2014-01-01 07:02:14'
)
);
$check=json_encode($data);
$postdata = "&data=$check&apikey=$apikey&method=$method";
$ch = curl_init();
curl_setopt_array(
$ch,
array(
CURLOPT_URL => $url.'/webservice/',
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_VERBOSE => true,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $postdata,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HTTPHEADER => array('Content-type: application/x-www-form-urlencoded',
)
)
);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
?>
Why not use mysql?... you can store json string on db (as..strings). I dont follow what you are trying to do.