Why message will not be sent? - api

webhook is set...
I want to send a message to the robot users in response to any message from them
AND
I really want to use cURL
$content = file_get_contents("php://input");
$update = json_decode($content, true);
$chat_id = $update['message']['chat']['id'];
echo $chat_id;
$ch = curl_init();
$url = "https://api.telegram.org/bot" . bot_token . "/";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, "sendMessage?chat_id=" . $chat_id . "&text=hello");
$res = curl_exec($ch);
curl_close($ch);

sendMessage is URL Path, not POST Body.
URL should be .../bot123:AA/sendMessage.

Related

Whatsapp Cloud Api get Media Object ID

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"];
}

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 create a table from this ForEach Loop?

I am trying to fetch all the domains I have purchased from Godaddy account using their API. However, It's not showing in a table format for proper view. Can anybody guide me on how to create a table out of this API results? Currently, it's showing results each at a different line but I prefer to view them in a table format.
Thanks!!
<?php
$domains = getDomains();
// check if error code
if(isset($domains['code'])){
$msg = explode(":",$domains['message']);
$msg = '<h2 style="text-align:center;">'.$msg[0].'</h2>';
// proceed if no error
} else {
$msg = '';
foreach ($domains as $domain){
$msg .= $domain['domain'].'</br>';
$msg .= $domain['createdAt'].'<br>';
$domain['createdAt'];
$domain['domain'];
$domain['domainId'];
$domain['expirationProtected'];
$domain['expires'];
$domain['holdRegistrar'];
$domain['locked'];
$domain['nameServers'];
$domain['privacy'];
$domain['renewAuto'];
$domain['renewDeadline'];
$domain['renewable'];
$domain['status'];
$domain['transferProtected'];
}
}
echo $msg;
function getDomains(){
$status = 'ACTIVE';
$limit = '999';
$url = "https://api.godaddy.com/v1/domains?statuses=$status&limit=$limit";
// set your key and secret
$header = array(
'Authorization: #########'
);
//open connection
$ch = curl_init();
$timeout=60;
//set the url and other options for curl
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); // Values: GET, POST, PUT, DELETE, PATCH, UPDATE
//curl_setopt($ch, CURLOPT_POSTFIELDS, $variable);
//curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
//execute call and return response data.
$result = curl_exec($ch);
//close curl connection
curl_close($ch);
// decode the json response
$dn = json_decode($result, true);
return $dn;
}
?>

shorte.st new api convert to php curl

shorte.st is a url shorten service.
recently they changed the api as following:
curl commandline
curl H "public-api-token: fakekey" -X -d "urlToShorten=google.com" PUT http://api.shorte.st/v1/data/url
response:
{"status":"ok","shortenedUrl":"http:\/\/sh.st\/XXXX"}
How to change it into php curl version?
function shst($url){
$apiurl="https://api.shorte.st/v1/data/url";
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.
curl_setopt($ch, CURLOPT_URL, $apiurl);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT' );
curl_setopt($ch, CURLOPT_HTTPHEADER, array('public-api-token: fakekey','X-HTTP-Method-Override: PUT'));
curl_setopt($ch, CURLOPT_POSTFIELDS, "urlToShorten=".$url);
$data = curl_exec($ch);
curl_close($ch);
$obj = json_decode($data);
$ret=$obj->{'shortenedUrl'};
return $ret;
}
The sample they provided used the wrong url. should be https not http
This works:
function shst($url){
$key="";//your key
$curl_url = "https://api.shorte.st/s/".$key."/".$url;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $curl_url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
$array = json_decode($result);
$shortest = $array->shortenedUrl;
return $shortest;
}

Shopify XML Post in PHP

I am having trouble posting XML data using PHP and Curl to Shopify. I have:
$xml = '<?xml version="1.0" encoding="UTF-8"?><variant><id type="integer">260293006</id><fulfillment-service>manual</fulfillment-service><inventory-management>shopify</inventory-management><inventory-policy>deny</inventory-policy><sku>s136</sku><inventory-quantity type="integer">48</inventory-quantity><price>17.95</price></variant>';
$url = 'https://' . $API_KEY . ':' . $PASSWORD . '#' . $STORE_URL . '/admin/variants/#260293006.xml';
My code is:
$session = curl_init();
curl_setopt($session, CURLOPT_URL, $url);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($session, CURLOPT_MAXREDIRS, 3);
curl_setopt($session, CURLOPT_POST, 1);
curl_setopt($session, CURLOPT_POSTFIELDS, $xml);
curl_setopt($session, CURLOPT_HTTPHEADER, array('Content-Type: text/xml; charset=utf-8'));
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($session);
echo $response;
curl_close($session);
What is returned by Shopify page with this title:
<title>Shopify ยป Please Log In</title>
I think I am probably missing something obvious. Once I get this function to work, everything else should be easy to build. Thanks so much.
Thanks everyone. Based on this feedback, I was able to solve the problem. Major issues:
The # is not needed as user-457786 suggested
It needs to be a PUT method, not POST, which I found based on user-457786's link
A few other changes to the CURL:
$fp = tmpfile();
fwrite($fp, $xml);
fseek($fp, 0);
$session = curl_init();
curl_setopt($session, CURLOPT_URL, $url);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session, CURLOPT_PUT, true);
curl_setopt($session, CURLOPT_BINARYTRANSFER, true);
curl_setopt($session, CURLOPT_INFILE, $fp); // file pointer
curl_setopt($session, CURLOPT_INFILESIZE, strlen($xml));
curl_setopt($session, CURLOPT_HTTPHEADER, array('Content-Type: application/xml'));
$response = curl_exec($session);
fclose($fp);
curl_close($session);
I think # is not needed in $url, It should be
$url = 'https://' . $API_KEY . ':' . $PASSWORD . '#' . $STORE_URL . '/admin/variants/260293006.xml';