I've been killing myself the last few days trying to figure out why get_headers suddenly stopped working. I checked my INI file, I tried cUrl, I did everything I could see and finally realized that something must be mysteriously wrong with the string input itself.
When I type this:
if (filter_var($url, FILTER_VALIDATE_URL)!== false)
die(json_encode(array('error' => "Your image link doesn't seem to be valid. Are you sure you copied the whole URL?")));
echo gettype($url).'::::';
$url = 'http://www.manatees.net/manateeb.jpg';
echo $url;
echo gettype($url);
print_r(get_headers($url,1));
Here's the output:
string::::http://www.manatees.net/manateeb.jpgstringArray
(
[0] => HTTP/1.1 200 OK
[Date] => Tue, 22 Jul 2014 08:40:43 GMT
[Server] => Apache/2.2.22 (Debian)
.... and so on
But if I take out the hardcoded URL line and just let it use the value that has been passed via AJAX to my function (comment out the two lines like so):
if (filter_var($url, FILTER_VALIDATE_URL)!== false)
die(json_encode(array('error' => "Your image link doesn't seem to be valid. Are you sure you copied the whole URL?")));
//echo gettype($url).'::::';
//$url = 'http://www.manatees.net/manateeb.jpg';
echo $url;
echo gettype($url);
print_r(get_headers($url,1));
This is what I see instead in the console:
http//www.manatees.net/manateeb.jpgstring<br />
<font size='1'><table class='xdebug-error xe-warning' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Warning: get_headers(): This function may only be used against URLs in E:\Stuff\Working\htdocs\image_upload.php on line <i>180</i></th></tr>
I'm sending the same URL as I post manually here, but the result is different. The type is still correct, but could the characterset be changed somehow?
Here's the ajax call:
$.ajax({
beforeSend: spinnerOn(),
url: 'image_upload.php',
type: 'POST',
data: data,
cache: false,
dataType: 'json',
processData: false, // Don't process the files
contentType: false, // Set content type to false as jQuery will tell the server its a query string request
And here's the code that creates "data" (I don't remember why I did it this way):
var data = new FormData(); // create formdata (HTML5 object for uploading) object
data.append('action', 'upload'); // Depending on why the image was uploaded, perform different actions
data.append('url', theURL);
And of course, theURL = http://www.manatees.net/manateeb.jpg.