I'm building an API using PHP.
I want to PUT data from json file.
And have a json file over 1000 SKU
{"data":[
{
"sku": "ZT006V",
"w_quantities": [
{
"w_id": 460,
"qty": 10},
{
"w_id": 2454,
"qty": 10}
]
},
{
"sku": "ZT006XXX",
"w_quantities": [
{
"w_id": 454,
"qty": 12
}]
}
]}
I do a PUT API command with ti.json content.
And I get error: 20 SKU limit per submission
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => '/products/updateSkus',
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 =>file_get_contents('ti.json'),
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer xxx',
'Content-Type: application/json'
),
));
$response = curl_exec($curl)
curl_close($curl);
echo $response;
I wonder, is there a way to send all 1000+ SKUs in the json file?
I searched the net, but no solution worked for me.
regards
You manipulate the JSON File data on the fly using an array of numbers, which if number presents the ID, if I understand currently.
Related
I am trying to loop an array of fixtures using foreach and this works fine. However I want to pick the {fixtuteID} from the fixtures loop and put it in the predictions api endpoint
Api call for fixtures
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api-football-v1.p.rapidapi.com/v3/fixtures?date=2xx1-06-10&timezone=XXXXX",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => array(
'x-rapidapi-key: XxXxXxXxXxXxXxXxXxXxXxXx',
'x-rapidapi-host: xvvvxxx.io'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
Api call for predictions which requires {fixtuteID} from the above
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api-football-v1.p.rapidapi.com/v3/predictions?fixture={fixtuteID}",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => array(
'x-rapidapi-key: XxXxXxXxXxXxXxXxXxXxXxXx',
'x-rapidapi-host: xvvvxxx.io'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
Below is the loop I would like.
fixture 1 and prediction-fixture 1
fixture 2 and prediction-fixture 2
fixture 3 and prediction-fixture 3
fixture 4 and prediction-fixture 4
end loop
EDIT: my loop
$fixture = curl_exec($curl);
$fs = json_decode($fixture, true);
$fixtures = $fs['response'];
//get error
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
foreach($fixtures as $fx){
//fixture ID {fixtuteID}
echo $fx['fixture']['id'];
//I wand to add the prediction here like this-- echo $pr['prediction']."<br>";
}
}
And that loop gives me
fixture 1
fixture 2
fixture 3
fixture 4
EDIT I want to get the
"predictions->winner->id": 2232,->name": "Sao Jose",->comment": "Win or draw"
},
The json response from the predictions call is as below.
{
"get": "predictions",
"parameters": {
"fixture": "840715"
},
"errors": [
],
"results": 1,
"paging": {
"current": 1,
"total": 1
},
"response": [
{
"predictions": {
"winner": {
"id": 2232,
"name": "Sao Jose",
"comment": "Win or draw"
},
"win_or_draw": true,
"under_over": null,
"goals": {
"home": "-2.5",
"away": "-1.5"
},
"advice": "Double chance : Sao Jose or draw",
"percent": {
"home": "50%",
"draw": "50%",
"away": "0%"
}
},
"league": {
"id": 75,
"name": "Serie C",
"country": "Brazil",
"logo": "https://media.api-sports.io/football/leagues/75.png",
"flag": "https://media.api-sports.io/flags/br.svg",
"season": 2022
},
where I want to pick predictions -> winner -> id for each fixture in the loop.
It's not entirely clear where you got stuck with this, but it seems like the neatest way would be to put the cURL code in a function and then call the function from the loop. (Of course you could put the cURL code directly in the looop but it would clutter it up a bit.)
foreach($fixtures as $fx)
{
//fixture ID
echo $fx['fixture']['id'];
echo getPrediction($fx["fixture"]["id"])."<br>";
}
function getPrediction($fixtureID)
{
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api-football-v1.p.rapidapi.com/v3/predictions?fixture=
$fixtureID",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => array(
'x-rapidapi-key: XxXxXxXxXxXxXxXxXxXxXxXx',
'x-rapidapi-host: xvvvxxx.io'
),
));
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
I want to create envelope using DocuSign API in my web application. When I run it in postman then response showing object move with html content
This is my php code sample
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://account-d.docusign.com/v2.1/accounts/b15b77b0-9345-4780-bfc1-440b37991820/envelopes?change_routing_order=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 =>'{
"documents": [
{
"documentBase64": "Base64 code of my pdf file",
"documentId": "5865888",
"fileExtension": "pdf",
"name": "test pdf"
}
],
"emailSubject": "test pdf",
"recipients": {
"signers": [
{
"name": "User full name",
"email": "user#gmail.com",
"recipientId": "8959555"
}
]
},
"status": "send "
}',
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
'Authorization: [{"key":"Authorization","value":"Bearer {{accessToken}}"}]'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
My API response is
Object moved to here.
Please guide me how to solve the issue
Thank You
That base URL is incorrect. Account-d.docusign.com is only for authentication.
Requests should be made to {SERVER}/restapi/v2.1
So for you it would be demo.docusign.net/restapi/v2.1/accounts/{ACCOUNT_ID/envelopes
Here's the dev centre article showing the whole process
I am using laravel as a server API backEnd but when clients send request using cURL I got the following json decode exception
JSON_ERROR_SYNTAX
here is the code of client
$data = [ "items"=>array(
"order_reference"=> "123412",
"mode" =>"payment",
"products" => [array(
"id"=> 1,
"product_name"=> "product 1",
"quantity"=>1,
"unit_amount"=> 100)
]
, "currency"=>"YER",
"total_amount"=>1500,
"success_url"=> "https://company.com/success",
"cancel_url"=> "https://company.com/cancel",
"metadata"=>[
"Customer name"=> "somename",
"order id"=> 0 ]
)];
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://example.com/api/order",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30000,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode($data),
CURLOPT_HTTPHEADER => array(
"private-key: rRQ26GcsZzoEhbrP2HZvLYDbn9C9et",
"public-key: HGvTMLDssJghr9tlN9gr4DVYt0qyBy",
),
));
$response = curl_exec($curl);
and this is the code of API backEnd
json_decode($request->items,true);
and the result of the code is
JSON_ERROR_SYNTAX
It should be json_decode($response->items,true);
Add this header
Content-Type: application/json
to your client code in this manner:
CURLOPT_HTTPHEADER => array(
"private-key: rRQ26GcsZzoEhbrP2HZvLYDbn9C9et",
"public-key: HGvTMLDssJghr9tlN9gr4DVYt0qyBy",
"Content-Type: application/json"
)
then laravel will automatically decode in the backend and you don't need to use json_decode. You can simply use $request->items to access to the client's items.
How do I pass multidimensional associative array or a json as a payload to cURL request. I have been trying to wrap my head around this for sometime now without any success.
Here is what I have done so far. I make a curl request and pass a json to the CURLOPT_POSTFIELDS fields. This works alright. The problem is I want the code to run for multiple users which are in a json string saved in a variable
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://test.url/tokens',
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 =>'{
"customerName": "orland",
"mno": "Network",
"amount": "1",
"msisdn": "447911123456",
"description": "Awaiting",
"reference": "0fgdufgdfgdfs"
}',
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
The above code works fine.
But the payload for CURLOPT_POSTFIELDS is not fixed.
So this is what I tried doing, I encoded the json string and tried passing it to a for loop. I am sure the approach isn't bad, but I am not getting the syntax quite right. Here is my code.
<?php
$recip = '[{
"customerName": "Sorland",
"mno": "Network",
"amount": "1",
"msisdn": "447911123346",
"description": "Awaiting",
"reference": "0fgdufgdfgdfs"
},
{
"customerName": "Corland",
"mno": "MTN",
"amount": 1,
"msisdn": "447911123678",
"description": "Awaiting",
"reference": "0jsbfbsubfhbj"
},
{
"customerName": "orland",
"mno": "MTN",
"amount": 1,
"msisdn": "447911123111",
"description": "Awaiting",
"reference": "1234568djnfjnfjds"
}]';
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://test.url.com/api/',
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 =>
echo ("'{<br>");
foreach($arr[$keys[$i]] as $key => $value) {
echo implode($key . " : " . $value . ",<br>");
}
echo "}',<br>";
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
}
I want to use a for loop to make multiple curl request with different payloads, and the payloads are coming from the json data above. I converted the json to an array to be able to loop through and converted it back to a json for CURLOPT_POSTFIELDS.
This seems a simpler way to make multiple call to curl and save the responses, hope I got the right end of the stick this time :)
$recip =
'[
{"customerName": "Sorland", "mno": "Network", "amount": "1",
"msisdn": "447911123346","description": "Awaiting","reference": "0fgdufgdfgdfs"
},
{"customerName": "Corland","mno": "MTN","amount": 1,
"msisdn": "447911123678","description": "Awaiting","reference": "0jsbfbsubfhbj"
},
{"customerName": "orland","mno": "MTN","amount": 1,"msisdn": "447911123111",
"description": "Awaiting","reference": "1234568djnfjnfjds"
}
]';
// make a PHP datatype out of the JSON so you can use it
$recipients = json_decode($recip);
$responses = [];
foreach ( $recipients as $r) {
$curl = curl_init();
// convert $r back to json so it can be passed as param
$postfield = json_encode($r);
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://test.url.com/api/',
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 => $postfield,
CURLOPT_HTTPHEADER => array('Content-Type: application/json')
));
$response[] = curl_exec($curl);
// if you need to know which customer the response was for
// use this next line instead, for example
//$response[$r->customerName] = curl_exec($curl);
curl_close($curl);
}
// this is now an array of all responses
print_r($responses);
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();