Check direct image url from Bing Search API?

I am trying to use the Bing Search API to allow users to map bing images to a message embedded in a website, after they select an image from it, it stores it on the server for use. But sometimes with Bing Search, the results returned by 'MediaUrl' are not images.

Example 1) Search for the Nascar keyword
Returns two images:
1) www.betbigdc.com/wp-content/uploads/2010/02/nascar1.jpg <- this is actually an image, and I can work, it works in <img src=''> .
2) http://images4.fanpop.com/image/photos/23900000/NASCAR-nascar-23962589-425-425.jpg .. this url is actually uploaded to www.fanpop.com/clubs/nascar/images/23962589/ title / nascar-photo? l = true ..

Example 2) Searching for the keyword Jason Aldean
Returns two images:
1) www.greenobles.com/data_images/jason-aldean/jason-aldean-01.jpg <- the actual URL of the image, which works in <img src=''> .
2) http://www.cmtradiolive.com/wp-content/uploads/2009/10/jason-aldean.jpg a funny url that actually loads a web page.

 <?php // Search Bing Api $articles = sitesearch('Jason Aldean', $_SERVER['HTTP_HOST'], $accountKey, 4); $i = 0; // Process Results starting with reszing image within aspect ration to display to user foreach ($articles['d']['results'] as $article) { $i++; $dimensions = array( $article['Width'], $article['Height'] ); $dimensionsNew = array( 125, 125 ); // What scale do we need to go to $scaleRequired = min($dimensionsNew[0] / $dimensions[0], $dimensionsNew[1] / $dimensions[1]); if ($scaleRequired < 1) { $twidth = $dimensions[0] * $scaleRequired; $theight = $dimensions[1] * $scaleRequired; // Resize to $finalDimensions } else { $twidth = $article['Width']; $theight = $article['Height']; } // Display images resized within ration to =< 125PX echo "<img alt='bimage' width='$twidth' height='$theight' src='" . $article['MediaUrl'] . "' style='magin: 10px !important; border: thin solid #666666;' />&nbsp;&nbsp;"; } ?> 

Is there any way to verify that a given URL is or is not a direct URL ?!

+4
source share
2 answers

Is there a way to verify that a given URL is or is not a direct way to a URL ?!

You can also analyze the answer itself. It will look like this:

 /** * * @return boolean Whether image is "pure" */ function is_image_url($url, array $extensions){ $response = get_headers($url, 1); foreach($response as $key => $value){ foreach($extension as $ext){ if (stripos($value, $ext) !== FALSE){ return TRUE; } } } return FALSE; } //Usage: $extensions = array('jpg', 'gif', 'bmp'); if ( is_image_url('http://exampe.com/image.jpg', $extensions) === TRUE ){ // It is. Do some stuff here } 
+1
source

You can use curl to determine this.

  • When initializing, set CURLOPT_FOLLOWLOCATION to false in curl_setopt so curl does not follow the redirect.

  • CURLOPT_HEADER to true to include return header information.

  • Determine if there is a set of Location headers.

  • For URLs like http://www.cmtradiolive.com/wp-content/uploads/2009/10/jason-aldean.jpg that directly lead you to other content without going through, you can judge by the Content-type in return the title to see if the content is an image or not.

In more detail about curl : http://www.php.net/manual/en/book.curl.php

0
source

All Articles