html - PHP Image from url src returns 403 error and application/octet-stream -


pulling images url when ran never had before. header check returned 403 error , although images extensions listed .jpg returned application/octet-stream, , checking content type returned text/html.

i have read 403 "typically" prevent screen scrapping, on images.

i found odd view source of web page, see image src, , click on , return image browser, not via code.

is there way convert image url actual image? want pull height, width, size info images , save them folder on server.

$html = file_get_contents($url); $doc = new domdocument(); $doc->loadhtml($html);  $tags = $doc->getelementsbytagname('img'); foreach ($tags $tag){  $image_src = $tag->getattribute('src');  echo get_headers($image_src, 1); //returns 403 forbidden error  echo image_type_to_mime_type(exif_imagetype($image_src)); //returns application/octet-stream  $i = getimagesize($image_src); var_dump($i); //returns bool(false)  $c = curl_init(); curl_setopt($c, curlopt_returntransfer, true); curl_setopt($c, curlopt_customrequest, 'head'); curl_setopt($c, curlopt_header, 1); curl_setopt($c, curlopt_nobody, true); curl_setopt($c, curlopt_url, $image_src); curl_exec($c); echo $content_type = curl_getinfo($c, curlinfo_content_type); //returns text/html  } 

in experience when dealing images getting application/octet-stream when expect have mime type of image/jpeg, image/png, etc. due script not being able process image correctly, due incorrect php config. (for example having image bigger max file upload or post size gives mime of octet-stream)

using file_get_contents() on url, need ensure allow_url_fopen enabled, fopen allowed contents of url though local file. (php ini allow_url_fopen)

alternatively @ using curl download url , go there (look @ answer way of doing this). try both of config change , curl process see if yield same results.

however fact getting 403 error sounds on remote side not allowing retrieve images through specific request. correctly identified security attempt stop scraping. have tried using different website grab images from, or server under control?

hope here helps :)


Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -