PHP cURL fails to fetch images from website -
i've written small php script grabbing images curl , saving them locally. reads urls images db, grabs , saves file folder. tested , works on couple other websites before, fails new 1 i'm trying with. did reading around, modified script bit still nothing.
please suggest out for.
$query_products = "select * product"; $products = mysql_query($query_products, $connection) or die(mysql_error()); $row_products = mysql_fetch_assoc($products); $totalrows_products = mysql_num_rows($products); { $ch = curl_init ($row_products['picture']); $agent= 'mozilla/4.0 (compatible; msie 6.0; windows nt 5.1; sv1; .net clr 1.0.3705; .net clr 1.1.4322)'; curl_setopt($ch, curlopt_header, 0); curl_setopt($ch, curlopt_returntransfer, 1); curl_setopt($ch, curlopt_binarytransfer, 1); curl_setopt($ch, curlopt_useragent, 'mozilla/5.0 (windows nt 6.1; rv:2.0) gecko/20110319 firefox/4.0'); curl_setopt($ch, curlopt_ssl_verifypeer, false); curl_setopt($ch, curlopt_verbose, true); curl_setopt($ch, curlopt_returntransfer, true); curl_setopt($ch, curlopt_useragent, $agent); $rawdata = curl_exec ($ch); $http_status = curl_getinfo($ch, curlinfo_http_code); curl_close ($ch); if($http_status==200){ $fp = fopen("images/products/".$row_products['productcode'].".jpg", 'w'); fwrite($fp, $rawdata); fclose($fp); echo ' -- downloaded <a href="'.$row_products['picture'].'" target="_blank">'.$newname.'</a> local: <a href="images/products/'.$newname.'" target="_blank">'.$newname.'</a>'; } else { echo ' -- failed download <a href="'.$row_products['picture'].'" target="_blank">'.$row_products['picture'].'</a>'; } usleep(500); } while ($row_products = mysql_fetch_assoc($products));
your target website may require/check combination of things. in order:
- location. websites allow referer value (either site or no referer, prevent hotlinking)
- incorrect url
- cookies. yes, can checked
- authentication of sort
the way sniff normal request looks , mimic it. msie
user-agent string looks different genuine msie ua, however, , i'd consider changing exact copy of real 1 if you.
could curl output file (using setopt output stream) , telling error code getting, along url of image? me more precise.
also, 0 isn't success - it's failure
Comments
Post a Comment