Related
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.
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 am trying to send images with Whatsapp Cloud API. Using PHP, I am able to send normal text messages successfully.
When going through the docs, what does 'MEDIA_OBJECT_ID' mean ? An example would be great.
curl -X POST \
'https://graph.facebook.com/v13.0/FROM_PHONE_NUMBER_ID/messages' \
-H 'Authorization: Bearer ACCESS_TOKEN' \
-d '{
"messaging_product": "whatsapp",
"recipient_type": "individual",
"to": "PHONE_NUMBER",
"type": "image",
"image": {
"id" : "MEDIA_OBJECT_ID"
}
}'
thanks
First, you need to upload the media file to the WhatsApp Server then WhatsApp will respond back the MEDIA_OBJECT_ID
$target="/home/rishabh/uploads/myImage.png";
$mime=mime_content_type('myImage.png')
$file = new CURLFILE($target);
$file->setMimeType($mime);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://graph.facebook.com/v13.0/$phoneSid/media",
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("messaging_product" => "whatsapp", "type"=>$mime, "file"=> $file),
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer ".$whatsappToken
),
));
$resultWhatsAppMedia = json_decode(curl_exec($curl), true);
$httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
$MEDIA_OBJECT_ID = $resultWhatsAppMedia['id']; //MEDIA OBJECT ID
Now, you will get the media object id. You need to send the media from API
$FileName="Caption Name or Image Name";
$messageBody = [
"messaging_product"=>"whatsapp",
"recipient_type" => "individual",
"to" => "$to_number",
"type" => "image",
"image" => [
"id" => $MEDIA_OBJECT_ID, // MEDIA OBJECT ID
"caption" => $FileName,
]
];
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://graph.facebook.com/v13.0/FROM_PHONE_NUMBER_ID/messages',
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 => json_encode($messageBody),
CURLOPT_HTTPHEADER => array(
"Authorization:Bearer $YOUR_WHATSAPP_ACCESS_TOKEN",
'Content-Type: application/json'
),
));
$response = json_decode(curl_exec($curl), true);
$status_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
Here is the different type of media JSON
// for mp3
$messageBody = [
"messaging_product"=>"whatsapp",
"recipient_type" => "individual",
"to" => "$to_number",
"type" => "audio",
"audio" => [
"id" => $MEDIA_OBJECT_ID, // MEDIA OBJECT ID
]
];
// for pdf, doc, apk etc
$messageBody = [
"messaging_product"=>"whatsapp",
"recipient_type" => "individual",
"to" => "$to_number",
"type" => "document",
"document" => [
"id" => $MEDIA_OBJECT_ID, // MEDIA OBJECT ID,
"caption" => $fileName,
"filename" => $fileName,
]
];
// for mp4
$messageBody = [
"messaging_product"=>"whatsapp",
"recipient_type" => "individual",
"to" => "$to_number",
"type" => "video",
"video" => [
"id" => $MEDIA_OBJECT_ID, // MEDIA OBJECT ID,
"caption" => $media['fileName'],
]
];
$RequestJSON = json_encode($messageBody)
You need to upload the media to https://graph.facebook.com/v13.0/FROM_PHONE_NUMBER_ID/media
The response will give you the "MEDIA_OBJECT_ID"
OR
use image link instead
curl -X POST \
'https://graph.facebook.com/v13.0/FROM_PHONE_NUMBER_ID/messages' \
-H 'Authorization: Bearer ACCESS_TOKEN' \
-d '{
"messaging_product": "whatsapp",
"recipient_type": "individual",
"to": "PHONE_NUMBER",
"type": "image",
"image": {
"link" : "Image URL"
}
}'
Example using Postman
URL:
https://graph.facebook.com/{{Version}}/{{Phone-Number-ID}}/messages
OBJECT:
{
"messaging_product": "whatsapp",
"recipient_type": "individual",
"to": "5500900050006",
"type": "image",
"image": {
"link": "https://images.ecycle.com.br/wp-content/uploads/2021/05/20195924/o-que-e-paisagem.jpg"
}
}
Don't forget...
Some web server does not permit access to files...😉
Media Messages
To send a media message, make a POST call to /PHONE_NUMBER_ID/messages and attach a message object with type=image, document, audio, image, video, or sticker. Then, add a corresponding media object.
Sample request using image with link:
POST
Send Image Message by URL
To send a media message, make a POST call to /{{Phone-Number-ID}}/messages and attach a message object with type = image. Then, be sure to include the link to the image.
Send an audio message to your customers using a link to an image file.
Authorization : Bearer Token
BODY
{
"messaging_product": "whatsapp",
"recipient_type": "individual",
"to": "{{Recipient-Phone-Number}}",
"type": "image",
"image": {
"link": "http(s)://image-url"
}
}
example in PHP CURL
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://graph.facebook.com/%7B%7BVersion%7D%7D/%7B%7BPhone-Number-ID%7D%7D/messages',
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 =>'{
"messaging_product": "whatsapp",
"recipient_type": "individual",
"to": "{{Recipient-Phone-Number}}",
"type": "image",
"image": {
"link": "http(s)://image-url"
}
}',
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer {{User-Access-Token}}',
'Content-Type: application/json'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
RESPONSE:
{
"messaging_product": "whatsapp",
"contacts": [
{
"input": "48XXXXXXXXX",
"wa_id": "48XXXXXXXXX "
}
],
"messages": [
{
"id": "wamid.gBGGSFcCNEOPAgkO_KJ55r4w_ww"
}
]
}
https://developers.facebook.com/docs/whatsapp/cloud-api/guides/send-messages#media-messages
I recommend you to download the WhatsApp Cloud API Postman collection from you Developer Dashboard for future doubts:
Go to https://developers.facebook.com/apps/
Select your WhatsApp application
In left sidebar, go to WhatsApp -> Getting Started
Click on the button "Run in Postman" to open a full API collection of samples
By the way I recommend you install the PHP SDK for WhatsApp Cloud API:
compose require netflie/whatsapp-cloud-api
#apositivo, when putting the above code on postmen, i got the wamid.#######. but I didn't receive the media message? any particular reason? or should i use the media object id?
{
"messaging_product": "whatsapp",
"recipient_type": "individual",
"to": "{{Recipient-Phone-Number}}",
"type": "image",
"image": {
"link": "https://iprovider.pk/whatsappapi/1640191015925.jpg"
}
}
there are two ways to upload a media via link or an id.
To get an id or a link first you need to upload the media using the API which would return a media object with ID and a link.
I used the Whatsapp Ruby SDK for connecting with the API which looks like this:
uploaded_media = medias_api.upload(sender_id: SENDER_ID, file_path: "tmp/whatsapp.png", type: "image/png")
media = medias_api.media(media_id: uploaded_media.data&.id).data
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);