How do I check if a video exists on YouTube, using PHP? - php

How do I check if a video exists on YouTube, using PHP?

Youtube has support for the oEmbed format.
Compared to the xml responsed provided by Pascal MARTIN, mine has only to download 600 bytes against 3800 bytes, making it faster and less bandwidth cosuming (only 1/6 of the size).
function yt_exists($videoID) {
$theURL = "http://www.youtube.com/oembed?url=http://www.youtube.com/watch?v=$videoID&format=json";
$headers = get_headers($theURL);
return (substr($headers[0], 9, 3) !== "404");
}
$id = 'yyDUC1LUXSU'; //Video id goes here
if (yt_exists($id)) {
// Yep, video is still up and running :)
} else {
// These aren't the droids you're looking for :(
}

What about using Youtube's API?
After all, that would mean using some official, which is less likely to change than going with parsing some HTML page.
For more information: YouTube APIs and Tools - Developer's Guide: PHP
The Retrieving a specific video entry seems quite interesting: if you send a request to an URL like this one:
http://gdata.youtube.com/feeds/api/videos/videoID
(replacing "videoID" by the ID of the video, of course – "GeppLPQtihA" in your example), you'll get some ATOM feed if the video is valid; and "Invalid id" if it's not
And, I insist: this way, you rely on a documented API, and not on some kind of behavior that exists today, but is not guaranteed.

Here is the solution that I use to check if YouTube video exists using video id. This is C# code, but basically you can check if the thumbnail of the video exists, you will either get 200 or 404 which is very convenient.
private async Task<bool> VideoExists(string id)
{
var httpClient = new HttpClient();
var video = await httpClient.GetAsync($"https://img.youtube.com/vi/{id}/0.jpg");
return video.IsSuccessStatusCode;
}

Request the URLs with the HEAD method, like so:
HEAD /watch?v=p72I7g-RXpg HTTP/1.1
Host: www.youtube.com
HTTP/1.1 200 OK
[SNIP]
HEAD /watch?v=p72I7g-BOGUS HTTP/1.1
Host: www.youtube.com
HTTP/1.1 303 See Other
[SNIP]
Location: http://www.youtube.com/index?ytsession=pXHSDn5Mgc78t2_s7AwyMvu_Tvxn6szTJFAbsYz8KifV-OP20gt7FShXtE4gNYS9Cb7Eh55SgoeFznYK616MmFrT3Cecfu8BcNJ7cs8B6YPddHQSQFT7fSIXFHd5FmQBk299p9_YFCrEBBwTgtYhzKL-jYKPp2zZaACNnDkeZxCr9JEoNEDXyqLvgbB1w8zgOjJacI4iIS6_QvIdmdmLXz7EhBSl92O-qHOG9Rf1HNux_xrcB_xCAz3P3_KbryeQk_9JSRFgCWWgfwWMM3SjrE74-vkSDm5jVRE3ZlUI6bHLgVb7rcIPcg

A new way to get a YouTube video data after September 2021, is by cURL in PHP:
function getYouTubeData($videoId) {
$theURL = "https://www.youtube.com/oembed?url=http://www.youtube.com/watch?v=$videoId&format=json";
$curl = curl_init($theURL);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$body = curl_exec($curl);
curl_close($curl);
return json_decode($body, true);
}
Usage:
$ytData = getYouTubeData($video_id);
if (empty($ytData)) {
$error = 'YouTube movie data could not be fetched.';
}
$title = $ytData['title'];
Sample output:
Array
(
[title] => Use online tools available at Laminas Starter Kit - Laminas MVC
[author_name] => Divix
[author_url] => https://www.youtube.com/channel/UC6lBQpNdQH6cu0j15qhkCAg
[type] => video
[height] => 113
[width] => 200
[version] => 1.0
[provider_name] => YouTube
[provider_url] => https://www.youtube.com/
[thumbnail_height] => 360
[thumbnail_width] => 480
[thumbnail_url] => https://i.ytimg.com/vi/LjDdAcB9-Mo/hqdefault.jpg
[html] => <iframe width="200" height="113" src="https://www.youtube.com/embed/LjDdAcB9-Mo?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
)

As commented by #dbro, the answer by Pascal MARTIN was an acceptable answer at that time. However, since the API had moved forward, fixed and improved, a new solution that works is the following. Please take note that this is based on the technique provided by #Pascal and I quote:
...if you send a request to an URL like this one
http://gdata.youtube.com/feeds/api/videos/videoID
(Replacing "videoID" by the idea of the video, of course -- "GeppLPQtihA" in your example)
You'll get some ATOM feed (**STOP HERE**)
The new URL to use and this is for V3 of the API is https://www.googleapis.com/youtube/v3/videos?id={the_id_of_the_video}&key={your_api_key}&part={parts}
WHERE
{the_id_of_the_video} you should know what this is
{your_api_key} is your app's key that can be found in your Developer Console
{parts} a comma-separated list, check here for valid values
Now for the Result
If the Video Id is VALID you will get data in the items field that includes the Id of the video and the information you queried through the parts parameter.
If the Video Id is NOT VALID then you will get an empty items.
Supplying a wrong key gives you an ERROR 400 (an error object).

You should request to this URL
https://www.googleapis.com/youtube/v3/videos?id={the_id_of_the_video}&key={your_api_key}&part=status
After that, you will receive the response json that contains uploadStatus field
{
etag = "\"I_8xdZu766_FSaexEaDXTIfEWc0/8QgL7Pcv5G8OwpNyKYJa8PaQTc0\"";
items = (
{
...
status = {
embeddable = 1;
license = youtube;
privacyStatus = public;
publicStatsViewable = 1;
uploadStatus = processed;
};
}
);
...
}
And there are 5 possible value for uploadStatus
deleted, failed, processed, rejected, uploaded
For uploadStatus = processed or uploaded => your youtube video is available

Found this solution on github:
Check if youtube video exists
Easy to use:
$headers = get_headers('http://www.youtube.com/oembed?url=http://www.youtube.com/watch?v=nonexistingid');
if (!strpos($headers[0], '200')) {
echo "The YouTube video you entered does not exist";
}
Working fine.

Here is a solution that doesn't involve using youtube api, it checks if the video id exists when the url is loaded
function checkYoutubeUrlIsValid($url) {
$buffer = file_get_contents($url);
$matches = [];
preg_match('#[a-zA-Z0-9_-]{11}$#', $url, $matches);
return strpos($buffer, $matches[0]) !== false;
}
Hope that helps

You want to validate if a youtube url is an url to a real youtube video? This is quite hard, you could use regular expressions, but keep in mind that there are loads of valid ways to express a youtube url:
http://www.youtube.com/watch?v=p72I7g-RXpg
http://www.youtube.com/watch?asv=76621-2&v=p72I7g-RXpg
http://www.youtube.com/v/RdPxlTX27Fk
etc.
Also the video code can contain alphanumeric characters, underscores, -characters (dunno what they are called) and possibly more.

Another (kind of inefficient) way is to use cURL to get the HTML of the supposed video page and run some regular expressions to verify that it's an actual video page.

http://www.youtube.com/watch?v=bQVoAWSP7k4
http://www.youtube.com/watch?v=bQVoAWSP7k4&feature=popular
http://www.youtube.com/watch?v=McNqjYiFmyQ&feature=related&bhablah
http://youtube.com/watch?v=bQVoAWSP7k4
var matches = $('#videoUrl').val().match(/http:\/\/(?:www\.)?youtube.*watch\?v=([a-zA-Z0-9\-_]+)/);
if (matches) {
alert('valid');
} else {
alert('Invalid');
}

/**
* Check youtube url, check video exists or not,
*
* #param $url full youtube video url
*
* #return string - yotube video id
*/
public static function checkYoutube($url)
{
if (preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $url, $match))
{
$headers = get_headers('http://gdata.youtube.com/feeds/api/videos/' . $match[1]);
if (strpos($headers[0], '200'))
{
return $match[1];
}
return false;
}
return false;
}
link:
https://github.com/DimitriMikadze/php-helpers

I used the YouTube API for checking if a video exists on You Tube. I downloaded the Google API Client Library for PHP. I used the following function:
/**
* Used to check if the given movie is availabe on youtube
*
* It uses youtube api and checks if given movie is available on youtube
* If a movie is not available then it returns false
*
* #param string $youtube_video_url the youtube movie url
*
* #return boolean $is_available indicates if the given video is available on youtube
*/
private function IsMovieAvailable($youtube_video_url)
{
/** The autoload.php file is included */
include_once("autoload.php");
/** Is available is set to false */
$is_available = false;
/** The youtube video id is extracted */
$video_id = str_replace("https://www.youtube.com/watch?v=", "", $youtube_video_url);
$DEVELOPER_KEY = $google_api_key;
$client = new \Google_Client();
$client->setDeveloperKey($DEVELOPER_KEY);
// Define an object that will be used to make all API requests.
$youtube = new \Google_Service_YouTube($client);
// Call the search.list method to retrieve results matching the specified
// query term.
$searchResponse = $youtube->videos->listVideos('status', array('id' => $video_id));
/** Each item in the search results is checked */
foreach ($searchResponse['items'] as $video) {
/** If the video id matches the given id then function returns true */
if ($video['id'] == $video_id) {
$is_available = true;
break;
}
}
return $is_available;
}

Here's a quick simple faster solution using the HEAD request method.
function check_youtube_video_exists($video_url) {
if (strpos($video_url, 'youtube.com') > 0 || strpos($video_url, 'youtu.be') > 0) {
$video_url = 'https://www.youtube.com/oembed?url='. $video_url .'&format=json';
}
$headers = #get_headers($video_url);
return (strpos($headers[0], '200') > 0) ? true : false;
}
Check your YouTube URL like so:
if (check_remote_video_exists('YOUR_YOUTUBE_VIDEO_URL')) {
// video exists, do stuff
} else {
// video does not exist, do other stuff
}

Related

My openstack temporary URL keeps being invalid

I try to generate temporary URLs for my Openstack ObjectStorage in a PHP application.
I've followed their documentation but even their python example isn't working.
So far, here is my generator :
class TempUrlGenerator implements ITempUrlGenerator
{
//See https://docs.openstack.org/swift/latest/api/temporary_url_middleware.html for details.
public function generate(string $url, int $validity = 5): string
{
$timestamp = time() + ($validity * 60);
return sprintf(
'%s?temp_url_sig=%s&temp_url_expires=%s',
$url,
$this->generateSignature($url, $timestamp),
$timestamp
);
}
private function generateSignature(string $url, int $timestamp): string
{
$body = sprintf(
'%s\n%s\n%s',
'GET',
$timestamp,
$this->getPath($url)
);
return hash_hmac('sha1', $body, trim(getenv('OPENSTACK_TEMPURL_KEY')));
}
private function getPath(string $url): string
{
$exploded = explode('/v1/', $url);
return sprintf('/v1/%s', $exploded[1]);
}
}
URL is the whole, complete URL (https://myserver.host.com/v1/...), validity is the number of minutes I want to keep my URL valid.
Beside that, the env variable is my secret key. I've already checked using a HEAD call on my account and my containers that the key is, indeed, uploaded on them with the good header. (X-Account-Meta-Temp-URL-Key and X-Container-Meta-Temp-URL-Key).
I've also checked multiple examples and implementations.
Yet I keep getting 401 invalid temp url when I try them.
Do you have any clue why or how I can troubleshot that ? Maybe is there some kind of setting on our server to check ?
Regards,

Behat + mink + Selenium2 Get Current Browser Name

How can I get the current browser name being used? I'm using Chrome and Firefox with my feature tests going under features -> chrome, features -> firefox. I want to take screenshots when a test fails. When I generate a screenshot, it just saves in my root dir. I want the file to be saved under the appropriate browser when I do this:
print_r($this->getSession()->getDriver())
I get that it has a private variable of browserName, but how do I access it?
[browserName:Behat\Mink\Driver\Selenium2Driver:private] => chrome
[desiredCapabilities:Behat\Mink\Driver\Selenium2Driver:private] => Array
(
[browserName] => chrome
[browser] => chrome
[version] => 25
[platform] => ANY
This is what worked for me.
$session = $this->getSession();
$driver = $session->getDriver();
$userAgent = $driver->evaluateScript('return navigator.userAgent');
$provider = $driver->evaluateScript('return navigator.vendor');
$browser = null;
if (preg_match('/google/i', $provider)) {
//using chrome
$browser = 'chrome';
} elseif (preg_match('/firefox/i',$userAgent)) {
$browser = 'firefox';
}
Have you tried executing
driver.executeScript("return navigator.userAgent;")
and parse the user agent string to get this information ?
This should help you get the browser name, its version, the OS information etc.,
Try this
/**
* Returns current browser name.
*
* #return string Browser name.
*/
function getBrowserName(): string
{
return $this->getCapabilities()['browserName'];
}

Signed URL for google bucket does not match signature provided

I'm having an hard time working with google storage.
So I'm trying to make a signed url, already have the client id (which is an email) and private key (as described here) so:
STEP 1: construct the string
function googleBuildConfigurationString($method, $expiration, $file, array $options = []) {
$allowedMethods = ['GET', 'HEAD', 'PUT', 'DELETE'];
// initialize
$method = strtoupper($method);
$contentType = $options['Content_Type'];
$contentMd5 = $options['Content_MD5'] ? base64_encode($options['Content_MD5']) : '';
$headers = $options['Canonicalized_Extension_Headers'] ? $options['Canonicalized_Extension_Headers'] . PHP_EOL : '';
$file = $file ? $file : $options['Canonicalized_Resource'];
// validate
if(array_search($method, $allowedMethods) === false) {
throw new RuntimeException("Method '{$method}' is not allowed");
}
if(!$expiration) {
throw new RuntimeException("An expiration date should be provided.");
}
return <<<TXT
{$method}
{$contentMd5}
{$contentType}
{$expiration}
{$headers}{$file}
TXT;
}
So far so good (I think), echoing the output it looks similar to the examples, so now to sign the string
STEP 2: signing the string
Initialy I was using openssl_public_encrypt, after searching around found that google-api-php-client has the Google_Signer_P12 (which actually uses openssl_sign), so the method looks like:
function googleSignString($certificatePath, $stringToSign) {
return (new Google_Signer_P12(
file_get_contents($certificatePath),
'notasecret'
))->sign($stringToSign);
}
And here I'm not sure if this is signing it correctly, finally building final url
STEP 3: building the URL
function googleBuildSignedUrl($serviceEmail, $file, $expiration, $signature) {
return "http://storage.googleapis.com{$file}"
. "?GoogleAccessId={$serviceEmail}"
. "&Expires={$expiration}"
. "&Signature=" . urlencode($signature);
}
But the opening the URL in the browser will retrieve:
<Error>
<Code>SignatureDoesNotMatch</Code>
<Message>
The request signature we calculated does not match the signature you provided. Check your Google secret key and signing method.
</Message>
<StringToSign>GET 1437470250 /example/video.mp4</StringToSign>
</Error>
I've added a gist with the final script to be easier to read
So any idea what am I doing wrong?
I've found the solution, there was a bug on the expiration date where I was doing:
$expiration = (new DateTime())->modify('+3h')->getTimestamp();
So I've changed h to hours so that it works now, like:
$expiration = (new DateTime())->modify('+3hours')->getTimestamp();
But that didn't quite solved it, the actual missing part is that the Google_Signer_P12::sign() requires it to be encoded on base64, which is specified on the google docs:
Google Cloud Storage expects the Base64 encoded signature in its APIs.
However I (wrongly) though that Google_Signer_P12::sign() already would do that, so after I understood that it was required I've changed the sign method to:
function googleSignString($certificatePath, $stringToSign)
{
return base64_encode((new Google_Signer_P12(
file_get_contents($certificatePath),
'notasecret'
))->sign($stringToSign));
}
And it is working now!!!
I've also updated the gist for anyone that wants to use it :)

Github API List all repositories and repo's content

If I was to go about displaying just MY github repositories and their contents on an external website how would i go about doing this? Are there any source code's you can provide me with, if not point me in the right direction? I'm quite a beginner to programming so any help is appreciated. Thank you everyone.
Taking a glance at their website
I glanced over relevant links- but still have no clue how I would accomplish this.
-Github List all Repo's
-Github List all Repo content
all of the previous answers are great. however if you are looking for a quick and dirty example of how to get a list of publicly available repos then check out my jsfiddle.
which uses this ajax call to list all of a users public repos:
$("#btn_get_repos").click(function() {
$.ajax({
type: "GET",
url: "https://api.github.com/users/google/repos",
dataType: "json",
success: function(result) {
for(var i in result ) {
$("#repo_list").append(
"<li><a href='" + result[i].html_url + "' target='_blank'>" +
result[i].name + "</a></li>"
);
console.log("i: " + i);
}
console.log(result);
$("#repo_count").append("Total Repos: " + result.length);
}
});
});
to see what kind of data is returned just check the console after clicking the button or you can install Google Chromes JSONView extension and then just visit the url that the ajax request is making i.e. https://api.github.com/users/google/repos
Here is a nice way just with the curl. You should change the $user and the $token variableso to make this script work for your case. The code is tested with a valid token so I hope it will work for you. As you could see in the comments of the code the token could be generated from your github account from here https://github.com/settings/applications
<?php
// for example your user
$user = 'flesheater';
// A token that you could generate from your own github
// go here https://github.com/settings/applications and create a token
// then replace the next string
$token = 'ced38b0e522a5c5e8ab10';
// We generate the url for curl
$curl_url = 'https://api.github.com/users/' . $user . '/repos';
// We generate the header part for the token
$curl_token_auth = 'Authorization: token ' . $token;
// We make the actuall curl initialization
$ch = curl_init($curl_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// We set the right headers: any user agent type, and then the custom token header part that we generated
curl_setopt($ch, CURLOPT_HTTPHEADER, array('User-Agent: Awesome-Octocat-App', $curl_token_auth));
// We execute the curl
$output = curl_exec($ch);
// And we make sure we close the curl
curl_close($ch);
// Then we decode the output and we could do whatever we want with it
$output = json_decode($output);
if (!empty($output)) {
// now you could just foreach the repos and show them
foreach ($output as $repo) {
print '' . $repo->name . '<br />';
}
}
?>
Also since we like github, we should cache the results in the end and fetch them once per day or so.
All these examples are just as pseudo without "authentication" and you can improve them yourself as you like;
<?php
// a simple way to get a user's repo
$res = file_get_contents("https://api.github.com/repos/qeremy/mii");
$res = json_decode($res);
print_r($res);
?>
stdClass Object
(
[language] => JavaScript
[merges_url] => https://api.github.com/repos/qeremy/mii/merges
[contributors_url] => https://api.github.com/repos/qeremy/mii/contributors
[assignees_url] => https://api.github.com/repos/qeremy/mii/assignees{/user}
[url] => https://api.github.com/repos/qeremy/mii
[description] => Multipurpose JavaScript Library
[ssh_url] => git#github.com:qeremy/mii.git
[comments_url] => https://api.github.com/repos/qeremy/mii/comments{/number}
[statuses_url] => https://api.github.com/repos/qeremy/mii/statuses/{sha}
[keys_url] => https://api.github.com/repos/qeremy/mii/keys{/key_id}
...
<?php
// getting a repo's README
$res = file_get_contents("https://api.github.com/repos/qeremy/mii/readme");
$res = json_decode($res);
print_r($res);
?>
stdClass Object
(
[_links] => stdClass Object
(
[self] => https://api.github.com/repos/qeremy/mii/contents/README.md
[git] => https://api.github.com/repos/qeremy/mii/git/blobs/49f0c4d5e25ac44921ba4372aebd76d2da5128e2
[html] => https://github.com/qeremy/mii/blob/master/README.md
)
[url] => https://api.github.com/repos/qeremy/mii/contents/README.md
[type] => file
[sha] => 49f0c4d5e25ac44921ba4372aebd76d2da5128e2
[path] => README.md
[size] => 8213
[encoding] => base64
[content] => QWN0dWFsbHksIEkga25vdyB0aGF0IHRoZXJlIGFyZSBidWNoIG9mIEphdmFT
Y3JpcHQgbGlicmFyeSwgZXZlbiBtb3JlIHBvd2VyZnVsbC4gQnV0IHNvbWV0
...
But, I think needs more complicated structure;
<?php
class GRepo
{
protected
// needs "user"
$src_userRepos = "https://api.github.com/users/%s/repos",
// needs "user,repo"
$src_userRepoDetails = "https://api.github.com/repos/%s/%s",
$responseCode, $responseText,
$user;
public function __construct($user) {
$this->user = $user;
}
public function listRepos() {
$this->_request(
sprintf($this->src_userRepos, $this->user));
if ($this->responseCode != 200) {
throw new Exception('Server error!'); // e.g
}
return json_decode($this->responseText);
}
public function getRepoDetails($repo) {
$this->_request(
sprintf($this->src_userRepoDetails, $this->user, $repo));
if ($this->responseCode != 200) {
throw new Exception('Server error!'); // e.g
}
return json_decode($this->responseText);
}
// Could be extended, e.g with CURL..
protected function _request($url) {
$contents =# file_get_contents($url);
$this->responseCode = (false === $contents) ? 400 : 200;
$this->responseText = $contents;
}
}
// Test
$gr = new GRepo('qeremy');
print_r( $gr->listRepos() );
print_r( $gr->getRepoDetails('mii') );
?>
When you say "display a repo and its contents" you actually say "display the state of the repo after the latest commit of the master branch", right? That's actually the better way of thinking about the problem and will be a better guide through using GitHub's API.
You need to look at the Git data part of the API. Here's what you need to do:
1) fetch the list of refs for your repo using:
https://api.github.com/repos/:user/:repo/git/refs
Working example:
https://api.github.com/repos/izuzak/noam/git/refs
Notice that it lists the references in your repo and gives you links to continue.
2) fetch the commit object of the ref that interests you, namely "master", using the link provided in the response to 1):
https://api.github.com/repos/:user/:repo/git/commits/:sha
Working example:
https://api.github.com/repos/izuzak/noam/git/commits/5cf12775b844664d5f7af6663706195680181374
Notice that you get back an object with a link to a tree.
3) fetch the tree object of the last commit in the master ref, using the link provided in the response to 2) :
https://api.github.com/repos/:user/:repo/git/trees/:sha
Working example:
https://api.github.com/repos/izuzak/noam/git/trees/8a721bea8d2f281c87b39c74cbf5a70075d686b4
Notice that you get back a list of files in the root directory that is your repo. This is what you want. If you have subdirectories, you will get links to fetch the files in those subdirectories.
This should be enough to get you started :). Good luck!
Please try following library also available on git hub:
https://github.com/ornicar/php-github-api
You need to parse the respone Githubs API sends you back. In PHP you can do this by using json_decode() which will give you an array to work with. You can use something like curl to issue the requests from PHP and then get the results and parse them as described above.
Another way to do this are REST Client classes for PHP, have a look at this one here for example.
If you want some source code to analyze, about javascript you can try to start from GitHub Repositories (more specifically here), it's a nice open project for a Chrome extension which does something similiar to what you're looking for.
you can use github api
organization="write-here-the-organization"
githubuser="your-github-user"
token=`curl -i -u ${githubuser} -d '{"scopes": ["repo"]}' https://api.github.com/authorizations | grep token | cut -d\" -f 4`
curl -i -H "Authorization: token ${token}" https://api.github.com/orgs/${organization}/repos
as result of the above you will get a long json with all repositories, and their information. you can continue from here.

Is there a way to check whether a remote image is exist? PHP

My site is running in LAMP, my image CDN is in nginx.
I want to do is:
Check if a requested image has a copy in CDN server, if yes then loan the copy in cdn server, otherwise, load the local copy for user.
Is there a programmatically way to check whether the remote CDN image is exist?
(perhaps determine the header? as I notice that if request image isn't exist, it returns 404)
I use this method to ping distant files:
/**
* Use HTTP GET to ping an url
*
* /!\ Warning, the return value is always true, you must use === to test the response type too.
*
* #param string $url
* #return boolean true or the error message
*/
public static function pingDistantFile($url)
{
$options = array(
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_URL => $url,
CURLOPT_FAILONERROR => true, // HTTP code > 400 will throw curl error
);
$ch = curl_init();
curl_setopt_array($ch, $options);
$return = curl_exec($ch);
if ($return === false)
{
return curl_error($ch);
}
else
{
return true;
}
}
You can also use the HEAD method but maybe your CDN as disabled it.
So long as the copy is public, you could just check for a 404 with cURL. See this question detailing how to do it.
You can use file_get_contents for this:
$content = file_get_contents("path_to_your_remote_img_file");
if ($content === FALSE)
{
/*Load local copy*/
}
else
{
/*Load $content*/
}
Oh and one more thing- if you only want to display the image with an img tag, you can simply do this- using img tags onerror attribute- if the image does not exist on the server, the onerror attribute will display the local file:
<img src="path_to_your_remote_img_file" onerror='this.src="path_to_your_local_img_file"'>
You can read a similar question on this here: detect broken image using php
Another easier way – without cURL:
$headers = get_headers('http://example.com/image.jpg', 1);
if($headers[0] == 'HTTP/1.1 200 OK')
{
//image exist
}
else
{
//some kind of error
}
<?php
if (is_array(getimagesize("http://www.imagelocation.com/image.png"))){
// Image ok
} else {
// Image not ok
}
?>

Categories