PHP how to check if openload video exist? - php

I need something to check if openload video exist, some videos sometimes get removed by DMCA report and i just need to display myself not working links.
Just a sketch what I wanna
$result = mysqli_query($db, "SELECT videos FROM table");
while($row=mysqli_fetch_assoc($result) {
$embedUrl = $row["videos"];
//so i wanna show only not working url's
if($embedUrl == false)
echo $embedUrl;
}
This is example of not working link here

Try this. Outputs: 'Video unavailable' if a video doesn't exist.
See comments for step-by-step explanation.
<?php
// Your Openload URL
$url = 'https://openload.co/embed/UgmaOAo1wlg/Horrible.Bosses.2.2014.720p.BluRay.x264.YIFY.mp4';
// Initialize cURL library.
if (($curl = curl_init()) === FALSE)
{
$errno = curl_errno();
throw new RuntimeException("curl_init() ($errno): " . curl_strerror($errno));
}
// Tell cURL which URL to operate on. GET is the default method.
curl_setopt($curl, CURLOPT_URL, $url);
// Optionally specify a path to a certificate store in PEM format.
// curl_setopt($curl, CURLOPT_CAINFO, __DIR__ . '/cacert.pem');
// Given Openload URL is requested over https. Allow for some sanity checking.
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, TRUE);
// Set this to the latest SSL standard supported by PHP at the time of this answer.
curl_setopt($curl, CURLOPT_SSLVERSION, 6);
// Return response, so we can inspect its contents.
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
// Openload returns HTTP code 200 if a video wasn't found. Any code >= 400 indicates a different problem.
curl_setopt($curl, CURLOPT_FAILONERROR, TRUE);
// Allow for server-side redirects.
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
// Don't include header in response.
curl_setopt($curl, CURLOPT_HEADER, FALSE);
if (($response = curl_exec($curl)) === FALSE)
throw new RuntimeException("curl_exec() failed for $url: " . curl_error($curl));
// Perform a case-insensitive search for a token that is specific to the 'video not found' page.
if (stripos($response, '<img class="image-blocked" src="/assets/img/blocked.png" alt="blocked">') !== FALSE)
echo 'Video unavailable';

Related

Checking file modification date for a local file against a web hosted file

I've get the file modification date for a local file with this code
<?php
$filename = 'db/ciupercomania.db';
if (file_exists($filename)) {
echo "This database was created on: <b>".date("F d Y H:i:s.", filemtime($filename)) . "</b><br/><br/>";
} else {
echo "File not exist.";
}
?>
but I'm struggling to get it for a dropbox hosted file, where I use this code:
<?php
$filename = 'https://www.dropbox.com/home/ciupercomania/Android_db?preview=Ciupercomania.db';
if (file_exists($filename)) {
echo "Out there is a database created on: <b>".date("F d Y H:i:s.", filemtime($filename)) . "</b><br/><br/>";
} else {
echo "File not exist.";
}
?>
I want to mention that I tried filectime as well as the dropbox link with extension ?dl=0 and ?dl=1 (automatic or by choice download) without success.
For the dropbox link I always get the FILE NOT EXIST message. If I don't echo it, I get a warning:
Warning: filemtime(): stat failed for https://www.dropbox.com/home/ciupercomania/Android_db?preview=Ciupercomania.db
Edit
Based on ADyson observation and his provided links, combined with other searches on this theme. I found out that filemtime() only works with files on the same server, and I should look for other solutions like curl_getinfo() or get_header().
Based on that I'm tried these codes:
<?php
$url = 'https://www.dropbox.com/home/ciupercomania/Android_db?preview=Ciupercomania.db';
$curl = curl_init($url);
//don't fetch the actual page, i only want headers
curl_setopt($curl, CURLOPT_NOBODY, true);
//stop it from outputting stuff to stdout
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// attempt to retrieve the modification date
curl_setopt($curl, CURLOPT_FILETIME, true);
// disabling SSL verification to a HTTPS website, I don't care about it.
// curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
// curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
// disabling SSL verification for a HTTPS website, I don't care about it.
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
//Tell cURL where my certificate bundle is located.
// $certificate = "cert/cacert.pem";
// curl_setopt($curl, CURLOPT_CAINFO, $certificate);
// curl_setopt($curl, CURLOPT_CAPATH, $certificate);
//Check for errors.
if(curl_errno($curl)){
throw new Exception(curl_error($curl));
}
$result = curl_exec($curl);
// if ($result === false) {
// die (curl_error($curl));
// }
$timestamp = curl_getinfo($curl, CURLINFO_FILETIME);
if ($timestamp != -1) { //otherwise unknown
echo date("Y-m-d H:i:s", $timestamp);
}
?>
and this one
<?php
$url = 'https://www.dropbox.com/home/ciupercomania/Android_db?preview=Ciupercomania.db';
$h = get_headers($url, 1);
$dt = NULL;
if(!$h || strpos($h[0], '200') !== false){
$dt = new \DateTime($h['Last-Modified']);
echo $dt;
}
?>
First I've got SSL verification error and for that I tried both, ignoring it with CURLOPT_SSL_VERIFYHOST, false or CURLOPT_SSL_VERIFYHOST, 0, or by pointing at it with:
$certificate = "cert/cacert.pem";
curl_setopt($curl, CURLOPT_CAINFO, $certificate);
but without any success.
I don't receive errors but neither a result.
Returning at ADyson's observation and since I'm really dumb, I would really appreciate it, if someone can point me to where I'm doing wrong and help me out.

Curl Drops Parameters

SO, I have been fighting with a piece of code that I want to use to get a remote page's source code using curl.
The code executes successfully, both in the browser and on command line. However, I get the of the main file only. When parameters are added, they are not considered whatsoever in the output.
The Code:
STACK : Ubuntu, Nginx, PHP-FPM 7.2
$urlcontent = 'https://XXX.YYY.COM/file/?var1=value1' ;
// Create a new cURL resource
$curl = curl_init();
if (!$curl) {
die("Couldn't initialize a cURL handle");
}
// Set the file URL to fetch through cURL
curl_setopt($curl, CURLOPT_URL, $urlcontent);
// Set a different user agent string (Googlebot)
curl_setopt($curl, CURLOPT_USERAGENT, 'CodiBot/2.1');
// Follow redirects, if any
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
// Fail the cURL request if response code = 400 (like 404 errors)
curl_setopt($curl, CURLOPT_FAILONERROR, true);
// Return the actual result of the curl result instead of success code
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// Wait for 10 seconds to connect, set 0 to wait indefinitely
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
// Execute the cURL request for a maximum of 50 seconds
curl_setopt($curl, CURLOPT_TIMEOUT, 50);
// Do not check the SSL certificates
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
// Fetch the URL and save the content in $html variable
$html = curl_exec($curl);
// Check if any error has occurred
if (curl_errno($curl))
{
echo 'cURL error: ' . curl_error($curl);
}
else
{
// cURL executed successfully
print_r(curl_getinfo($curl));
print_r($html);
}
curl_close($curl);
PROBLEM
I get the content for https://XXX.YYY.COM/file but not the corresponding ?var1=value1 part. IN other words, as I feed info to be retrieved to DB I get only the html of the main file.
I tried :
curl_setopt($ch, CURLOPT_POSTFIELDS, 'foo=1&bar=2&baz=3');
I know the remote server may have CORS enabled, but I tried the same url using a remote curl retriever and it succeeded. SO, it may not be the remote server

Cannot upload an image on imgur [API 3]

I'm trying to upload images on imgur but I often have problem and I am not able to upload the image.
In the code I'll publish I don't understand why I keep getting the boolean value: false as result of curl_exec($ch); and not a json string. From the PHP Manual it means that the post failed but I don't understand why.
Here I successfully read the image from a post request
$imageContent = file_get_contents($myFile["tmp_name"][$i]);
if ($imageContent === false) {
// Empty image - I never get this error
} else {
//Image correctly read
$url = $this->uploadLogged($imageContent);
}
While here is my attempt to upload it
public function uploadLogged($image){
$upload_route = "https://api.imgur.com/3/image";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $upload_route);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Authorization: Bearer '.$this->access_token));
curl_setopt($ch, CURLOPT_POSTFIELDS, array('image' => base64_encode($image)));
$response = curl_exec($ch);
$responseDecoded = json_decode($response);
curl_close ($ch);
$link = $responseDecoded->data->link;
if( empty($link) ){
throw new Exception("Cannot upload the image.<br>Response: ".json_encode($response));
}
return $link;
}
Moreover $this->access_token correspond to a valid access token
when curl_exec returns bool(false), there was an error during the transfer. to get an extended error description, use the curl_errno() and curl_error() functions. to get even more detailed info of the transfer, use the CURLOPT_VERBOSE and CURLOPT_STDERR options of curl_setopt. eg
$curlstderrh=tmpfile();
curl_setopt_array($ch,array(CURLOPT_VERBOSE=>1,CURLOPT_STDERR=>$curlstderrh));
$response = curl_exec($ch);
$curlstderr=file_get_contents(stream_get_meta_data($curlstderrh)['uri']);
fclose($curlstderrh);
if(false===$response){
throw new \RuntimeException("curl_exec failed: ".curl_errno($ch).": ".curl_error($ch).". verbose log: $curlstderr");
}
unset($curlstderrh,$curlstderr);
should get you both the libcurl error code, an error description, and a detailed log of what happened up until the error, in the exception message.
common issues include an SSL/TLS encryption/decryption error, timeout errors, and an unstable connection.

Icecast metadata update by php

I need to get Icecast metadata auto update by PHP let say every 15 min which will be done by cPanel cronjob.
i had the below code but it does't work(it works if I use header location to redirect however cronjob won't be able to do that)
<?PHP
$url="http://tgftp.nws.noaa.gov/data/observations/metar/stations/KJFK.TXT";
$info=file_get_contents($url);
$url_info = "http://username:password#icecast:8000/admin/metadata?mount=/mymount&mode=updinfo&song=" . urlencode($info);
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $url_info);
curl_setopt($ch, CURLOPT_HEADER, 0);
// grab URL and pass it to the browser
curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
?>
Try checking for errors after you execute the call using curl_error:
<?php
$url="http://tgftp.nws.noaa.gov/data/observations/metar/stations/KJFK.TXT";
$info=file_get_contents($url);
$url_info = "http://username:password#icecast:8000/admin/metadata?mount=/mymount&mode=updinfo&song=" . urlencode($info);
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $url_info);
curl_setopt($ch, CURLOPT_HEADER, 0);
// grab URL and pass it to the browser, check for errors
if (curl_exec($ch) === FALSE)
{
print 'Curl-Error occurred: ' . curl_error($ch).', error code: '.curl_errno($ch);
}
// close cURL resource, and free up system resources
curl_close($ch);

cURL returns empty output from valid url - no errors reported

If you just enter the urls into the browser you can see that both work, cdon works even without javascript, have they blocked cURL somehow?
I'm trying to build a scraper to benifit legal movies online which would benifit them a whole lot, seems stupid blocking scrapers in general imho. Although I'm far from sure that's whats going on here! Might be just an error somewhere..
// Works
get_file1('http://sfanytime.com/sv-SE/Sokresultat/?field=all&q=The+Matrix', '/', 'sfanytime.html');
// Saves a blank 0 KB file
get_file1('http://downloads.cdon.com/index.phtml?action=search&search_terms=The+Matrix', '/', 'cdon.html');
function get_file1($file, $local_path, $newfilename) {
$out = fopen($newfilename, 'wb');
if ($out === FALSE) {
return false;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_FILE, $out);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, $file);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch);
$error = curl_error($ch);
if (strlen($error) > 0) {
echo "<br>Error is : ". $error;
return false;
}
curl_close($ch);
return true;
}
You should change the line
curl_setopt($ch, CURLOPT_FAILONERROR, true);
...to...
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
CURLOPT_FAILONERROR will cause a "silent fail" - which from what you say, is not what you want. I have replaced this with CURLOPT_FOLLOWLOCATION, because when I visit the second URL, I get redirected to a "choose your country" type page, which will be a response with an empty body - which is why you get an empty file.
There is no problem with your code as such, simply a problem with the way you handle the response from the second URL. You don't see an error because, technically, there wasn't one.

Categories