How to create a table from this ForEach Loop? - godaddy-api

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;
}
?>

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);

Why message will not be sent?

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.

Find out URL response to check broken Url's in Codeception

I have fetched multiple URL's using grabMultiple() method.
I want to check its response to check whether it is broken(i.e.404) or not.
Can I use HTTP Response in Codeception ? If yes what is the syntax ?
Use seeResponseCodeIs method.
$links = $I->grabMultiple('div.test-info a', 'href');
foreach ($links as $link) {
$I->amOnPage($link);
$I->seeResponseCodeIs(\Codeception\Util\HttpCode::OK);
}
This worked for me -
function check_url($link){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $link);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10); //follow up to 10 redirections - avoids loops
$data = curl_exec($ch);
curl_close($ch);
preg_match_all("/HTTP\/1\.[1|0]\s(\d{3})/",$data,$matches);
$code = end($matches[1]);
if($code==404)
{
echo "Bad url";
}
else{
echo "Good url";
}
}

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