issue
If you using PHP:
$images = new Images;
$file = dirname(__FILE__) . "/media/media.jpg"; $image = $images->getImage($term); if($image !== false){ file_put_contents($file, $image); }
/* Class with functions */ class Images{ private function isNotJson(string){ json_decode(string); return json_last_error() !== JSON_ERROR_NONE; }
public function getImage($term){ $curl = curl_init(); curl_setopt_array($curl, [ CURLOPT_URL => "https://rapid-porn.p.rapidapi.com/photo?category=". $term, CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => "", CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 30, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => "GET", CURLOPT_HTTPHEADER => [ "X-RapidAPI-Host: rapid-porn.p.rapidapi.com", "X-RapidAPI-Key: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" ], ]); $response = curl_exec($curl); $err = curl_error($curl); curl_close($curl); if($err){ return false; }else{ if($this->isNotJson($response)){ return $response; }else{ return false; } } }
}
Join in the discussion - add comment below:
If you using PHP:
$images = new Images;
/*
Class with functions
*/
class Images{
private function isNotJson(string){ json_decode(string);
return json_last_error() !== JSON_ERROR_NONE;
}
}