Whatsapp Cloud Api get Media Object ID - api

I want to upload the media via Whatsapp cloud API in order to send the media to WhatsApp, I have followed the link below, but having the stated issue.
https://developers.facebook.com/docs/whatsapp/cloud-api/reference/media#get-media-id
Array ( [error] => Array ( [message] => (#100) The parameter messaging_product is required. [type] => OAuthException [code] => 100 [fbtrace_id] => AQPftb9Bf343375gQ_QytxNen ) ) 400
My Code is
// #1640191015925.jpg on my root folder
$post = [
'file' => '#1640191015925.jpg;type=image/jpeg',
'messaging_product' => 'whatsapp'
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://graph.facebook.com/v13.0/Phone-Number-ID/media');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$headers = array();
$YOUR_WHATSAPP_ACCESS_TOKEN = '+++++++';
$headers[] = "Authorization: Bearer $YOUR_WHATSAPP_ACCESS_TOKEN";
$headers[] = "Content-Type: image/jpeg";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = json_decode(curl_exec($ch), true);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
//$MEDIA_OBJECT_ID = $result['media'][0]['id']; //MEDIA OBJECT ID
print_r($result);
print_r($httpcode);
?> ```

I had the same issue, after some digging around I wrote this function to upload a file and return the id.
function waFileUploader($token, $file, $senderid){
$mime = mime_content_type($file);
$info = pathinfo($file);
$name = $info['basename'];
$curlfile = new CURLFile($file, $mime, $name);
$filedata = array("messaging_product"=>"whatsapp", "file" => $curlfile);
$url = 'https://graph.facebook.com/v13.0/' . $senderid . '/media';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER,array("Authorization: Bearer $token", "Content-Type: multipart/form-data"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $filedata);
$r = curl_exec($ch);
curl_close($ch);
$id = json_decode($r, true);
return $id["id"];
}

Related

Executing a cURL request to an background remover API from PHP throws file not found error

I am new to cURL from PHP. I am trying to send file URL as post field and API Key as header to the background remover API to retrieve the background removed image. The error says:
{"errors":[{"title":"No image given","code":"missing_source","detail":"Please provide the source image in the image_url, image_file or image_file_b64 parameter."}]}
My code:
<?php
$url='https://api.remove.bg/v1.0/removebg';
$ch = curl_init($url);
$data = array('image_url'=> 'https://www.requestingservicebyme.com/upload/imageexample.jpg');
$headers1=['X-API-Key:xxxxxxxxxxxxxxx',
'Content-Type:application/json'];
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$buffer = curl_exec($ch);
if (empty($buffer)) {
echo " buffer is empty ";
} else{
echo $buffer;
}
curl_close($ch);
?>
I was also working on it using the curl I got success in that ..!
Change your headers
Remove the application type json from headers as this api returns the data in Image content format.
$x = curl_init();
$headers=['X-API-Key:xxxxxxxxxxxxxxxxx'];
$data = array (
'image_url' => "https://amblin.com/wp-content/uploads/2019/09/castaway_2000_photo_29.jpg",
'size' => "auto",
);
$url = 'https://api.remove.bg/v1.0/removebg';
// $post = http_build_query($data);
$x = curl_init($url);
curl_setopt($x, CURLOPT_POST, true);
curl_setopt($x, CURLOPT_RETURNTRANSFER, true);
curl_setopt($x, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($x, CURLOPT_HTTPHEADER, $headers);
curl_setopt($x, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($x, CURLOPT_POSTFIELDS, $data);
$y = curl_exec($x);
curl_close($x);
//And then save it using this code
$fp = fopen("result.jpg", "wb");
fwrite($fp, $y);
fclose($fp);

How to update Products inventory from 3rd party POS system in Magento2?

How to update Products inventory from 3rd party POS system in Magento2?
I need to make a button in POS system when I hit that button the products inventory should be update from POS to Magento2 system.
Here is working code for update qty from Rest API
<?php $adminUrl = 'http://localhost/magento2/rest/V1/integration/admin/token/';
$ch = curl_init();
$data = array("username" => "admin", "password" => "admin"); //Admin Login details
$data_string = json_encode($data);
$ch = curl_init($adminUrl);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data_string)));
$token = curl_exec($ch);
$token= json_decode($token);
$headers = array("Authorization: Bearer $token","Content-Type: application/json");
$skus = array('100001' => 66,'100002' => 99); //Here 100001 and 100002 are SKU and 66 and 99 are Qty
foreach ($skus as $sku => $stock) {
$requestUrl='http://localhost/magento2/rest/V1/products/' . $sku . '/stockItems/1';
$sampleProductData = array("qty" => $stock);
$productData = json_encode(array('stockItem' => $sampleProductData));
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $requestUrl);
curl_setopt($ch,CURLOPT_POSTFIELDS, $productData);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);
unset($productData);
unset($sampleProductData);
}

Zoho crm API sometimes returns error

I'm trying to searchRecord using the Zoho CRM API and I sometimes get the following error:
Array ( [response] => Array ( [error] => Array ( [message] => Unable
to process your request. Please verify whether you have entered proper
method name,parameter and parameter values. [code] => 4600 ) [uri] =>
/crm/private/json/Contacts/searchRecords ) )
My problem is that sometimes everything works fine and sometimes I get this error
define("TARGETURL", "https://crm.zoho.com/crm/private/json/Contacts/searchRecords");
$parameter = array(
'scope' => 'crmapi',
'authtoken' => AUTHTOKEN,
'selectColumns' => 'All',
'criteria' => '(Account Name:'.$accountName.')',
'fromIndex' => $fromIndex,
'toIndex' => $toIndex
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, TARGETURL);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $parameter);
$result = curl_exec($ch);
curl_close($ch);
Try search field with double quotation mark, following is script to search email from lead module.
$xmlData = "(Email:$email)";
$ch = curl_init('https://crm.zoho.com/crm/private/xml/Leads/searchRecords');
curl_setopt($ch, CURLOPT_VERBOSE, 1); //standard i/o streams
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Turn off the server and peer verification
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set to return data to string ($response)
curl_setopt($ch, CURLOPT_POST, 1); //Regular post
$authtoken = "********************************";
$query = "authtoken=" . $authtoken . "&scope=crmapi&criteria=" . $xmlData;
curl_setopt($ch, CURLOPT_POSTFIELDS, $query); // Set the request as a POST FIELD for curl.
$response = curl_exec($ch);
curl_close($ch);

areto api payment gateway

I am trying to implement the Areto API payment gateway, but it returns response with authentication error such as :
Authentication failed - invalid ID/Session/IP combination.
I am try to first time implement (this type) payment gateway.
how to implement Areto API payment gateway.
Just have use the code like this:
<?php
$url = 'https://pay.aretosystems.com/api/sale/v1';
$api_session = 'ZXUKwKG97WCEdyYBZxnBgFOutVj5qdO7yWaYxT5T9CAbPMn4tamzUpH0HgtdIn6b';
$api_id = '6mK+KXQ5dadlu+4qnxpY3jtH/Hg56AgMhhpOMWXP0/TCmbE6OhXZkG/QRjE/dNjQ';
$transaction = array(
"Authentication" => array(
"Id" => $api_id,
"Session" => $api_session
),
"Order" => array(
"OrderId" => "TESTSALE",
"Amount" => "0.01",
"CurrencyCode" => "EUR"
),
"CardDetails" => array(
"CCVC" => "123",
"CCExpiryMonth" => "01",
"CCExpiryYear" => "2017",
"CCName" => "Wdp",
"CCSurname" => "Tech",
"CCNumber" => '4200000000000000',
"CCType" => "VISA",
"CCAdress" => "542, surya nagar",
"ClientCity" => "Jaipur",
"ClientCountryCode" => "US",
"ClientZip" => "302017",
"ClientEmail" => "wdpjaipur#gmail.com",
"ClientExternalIdentifier" => "MytestUserName1",
"ClientIP" => "162.158.50.205",
"ClientPhone" => "1234567890"
)
);
$data_string = json_encode($transaction);
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: application/json',
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$http_status . PHP_EOL;
$responceData = json_decode($response, TRUE);
echo '<pre>';
print_r($responceData);die;
?>
This part of the API is not very well documented, but after we found this question we were finally able to find a way to solve the problem:
The key is to have all the properties you want to send concatenated into a string divided by & characters and send them as plain text via POST.
If you want to try it out in Postman:
1) enter the properties as url parameters
2) copy the string from the url
3) and paste it in the request body, setting the type to plain text.
error_reporting(1);
$url = 'https://pay.aretosystems.com/api/sale/v1';
$api_session = 'ZXUKwKG97WCEdyYBZxnBgFOutVj5qdO7yWaYxT5T9CAbPMn4tamzUpH0HgtdIn6b';
$api_id = '6mK+KXQ5dadlu+4qnxpY3jtH/Hg56AgMhhpOMWXP0/TCmbE6OhXZkG/QRjE/dNjQ';
$clientIP = $_SERVER['REMOTE_ADDR'];
$request_arr = array(
'Id'=>$api_id,
'Session'=>$api_session,
'OrderId'=>time(),
'Amount'=>'4.0',
'CurrencyCode'=>'EUR',
'CCVC'=>'123',
'CCExpiryMonth'=>'12',
'CCExpiryYear'=>'2017',
'CCName'=>'wdp',
'CCSurname'=>'technologies',
'CCNumber'=>'4200000000000000',
'CCType'=>'VISA',
'CCAddress'=>'surya nagar',
'ClientCity'=>'Ajmer',
'ClientCountryCode'=>'IN',
'ClientZip'=>'302018',
'ClientEmail'=>'wdpjaipur1#gmail.com',
'ClientExternalIdentifier'=>'Wdp tech',
'ClientIP'=>$clientIP,
'ClientPhone'=>'1234567890',
'ClientDOB'=>'1989-11-05'
);
$ch = curl_init();
$post_variables = '';
foreach ($request_arr as $key => $value) {
$post_variables .= "{$key}={$value}&";
}
$post_variables = rtrim($post_variables, '&');
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_variables);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$response = curl_exec($ch);
curl_close($ch);
$responceData = json_decode($response, TRUE);

Updating Event Details using Eventrite API v3

I'm trying to edit Eventbrite event using version 3 of the API but I always returns false.
$request_url = 'https://www.eventbriteapi.com/v3/events/xxxxxx';
$options = array(
'http' => array(
'method' => 'POST',
'header'=> "Authorization: Bearer xxxxxxxxxx"
),
'name' => array(
'text'=>'Hi',
'html'=>'<p>Hi</p>'
));
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $options);
curl_setopt($ch, CURLOPT_URL, $request_url);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$json_data = curl_exec($ch);
$resp_info = curl_getinfo($ch);
curl_close($ch);
var_dump($json_data);
This always returns me :
boolean false
What am I doing wrong?
You don't need to create an options array with curl as you are setting those options in curl_setopt. Just make a data array and submit it.
<?php
$request_url = 'https://www.eventbriteapi.com/v3/events/xxxxxx';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $request_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
$data = array(
'name' => array(
'text'=>'Hi',
'html'=>'<p>Hi</p>'
)
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$json_data = curl_exec($ch);
$resp_info = curl_getinfo($ch);
curl_close($ch);
var_dump($json_data);
Though according to the docs there is a minimum amount of data to create an event.