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
Related
i have a curl command, its working fine in terminal and want to convert in PHP code but its giving 415 Unsupported Media Type error.
CURL Command:
curl --location
--request POST 'https://securegw-uat.starhealth.in/api/proposal-service/v2/ckyc/generate'
--header 'APIKEY: 25861f5655bd4640b5518ceb2bfb5f94'
--header 'SECRETKEY: beec6bf3afff4efdab1f6a80d8601fbf'
--form 'bodyJson="{\"ckycId\":10036801290855,\"idOrAddressProofDocumentId\": 1,\"titleId\": 1,\"firstName\": \"Swadhin\",\"middleName\": \"\",\"lastName\": \"Dhal\",\"genderId\": 1,\"birthdate\": \"November 01, 1973\",\"occupationId\": 1,\"residentialStatusId\": 1,\"pan\": \"BLAPT0864M\",\"familyRelationshipId\": 3,\"familyMemberTitleId\": 2,\"familyMemberFirstName\": \"Rachita\",\"familyMemberLastName\": \"Dhal\",\"addressLineOne\": \"No. 20\",\"addressLineTwo\": \"Nehru Street\",\"cityName\": \"Triplicane\",\"districtName\": \"Chennai\",\"stateCode\": \"TN\",\"countryCode\": \"IN\",\"postalCode\": \"600005\",\"incomeSourceId\": 1,\"isAnyonePEP\": 0}";type=application/json'
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://securegw-uat.starhealth.in/api/proposal-service/v2/ckyc/generate',
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('bodyJson' => '{
"ckycId": 10036801290855,
"idOrAddressProofDocumentId": 1,
"idOrAddressProofDocumentNumber": "1234",
"titleId": 1,
"firstName": "Swadhin",
"middleName": "",
"lastName": "Dhal",
"genderId": 1,
"birthdate": "November 01, 1973",
"occupationId": 1,
"residentialStatusId": 1,
"pan": "BLAPT0864M",
"familyRelationshipId": 3,
"familyMemberTitleId": 2,
"familyMemberFirstName": "Rachita",
"familyMemberLastName": "Dhal",
"addressLineOne": "No. 20",
"addressLineTwo": "Nehru Street",
"cityName": "Triplicane",
"districtName": "Chennai",
"stateCode": "TN",
"countryCode": "IN",
"postalCode": "600005",
"incomeSourceId": 1,
"isAnyonePEP": 0
}'),
CURLOPT_HTTPHEADER => array(
'Content-Type: multipart/form-data',
'APIKEY: 25861f5655bd4640b5518ceb2bfb5f94',
'SECRETKEY: beec6bf3afff4efdab1f6a80d8601fbf'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;`
Please try to specify the header to --header 'Content-Type: multipart/form-data'.
I am trying to send a message to Discord Webhook, with Components.
I have tried normal message and embed, and both give me the same result. I get the message, but not the components.
Syntax From here: https://discord.com/developers/docs/interactions/message-components
Here is my code:
<?php
$url = "https://discord.com/api/webhooks/1234567890";
$content = '{
"content": "This is a message with components.",
"components": [
{
"type": 1,
"components": [
{
"type": 2,
"label": "Click me!",
"style": 1,
"custom_id": "click_one"
}
]
}
]
}';
$curl = curl_init();
$headers = array('Content-Type: application/json');
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_POSTFIELDS => $content,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_HTTPHEADER => $headers,
CURLOPT_HEADER => true
));
$response = curl_exec($curl);
$httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
var_dump($response);
Here is the var_dump of the response:
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 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
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);