How to grap this specific url? - php

The code below(learned from Save image from url with curl PHP) works fine when try to grap image from internet. But when come to the url below, I only got a "test.jpg" which is actually a 404 error page("test.jpg" could be opened by notepad). PS: And i could open the url with browser and could see the image. Thanks to Mike,Problem solved and code updated.
$url = 'https://spthumbnails.5min.com/10368406/518420256_c_570_411.jpg';
$reffer="http://www.sohu.com";
$user_agent="Baiduspider+(+http://www.baidu.com/search/spider.htm)";
$saveto="test.jpg";
grab_image($url,$saveto);
function grab_image($url,$saveto,$reffer,$user_agent){
$ch = curl_init ($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
curl_setopt($ch,CURLOPT_REFERER,$reffer);
curl_setopt($ch,CURLOPT_USERAGENT,$user_agent);
$raw=curl_exec($ch);
curl_close ($ch);
$fp = fopen($saveto,'w');
fwrite($fp, $raw);
fclose($fp);
}

Thanks to Mike. This site do need "CURLOPT_REFERER" option(which i ignored) to grap the image. And I also add the useragent option to make sure it work on other situation.

Related

Display image from curl request in html<img> tag

One of the pages of a website i'm working on should display information about a manga such as it's cover image.
I'm trying to display an image I got by making a curl request.
<?php
if(isset($_GET['info'])) {
$postedData = $_GET["info"]; //json object containing info about manga such as author/title etc.
$info = json_decode($postedData, true);
$mangacoverid = $info['im']; //id of the cover image that i'm getting from json object
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://cdn.mangaeden.com/mangasimg/" . $mangacoverid); //link of API i'm using and adding cover id to it
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$picture = curl_exec($ch);
curl_close($ch);
header('Content-type: image/jpeg');
echo $picture; //displays image in browser
}
?>
-Some 'mangacoverid' for testing purposes:
ff/ff94bb880357b6b811bccbbfd3356c5ec41fbb184291323f0ed6a86a.jpg
c1/c1b0173d8986681f23ecf5a69d26aa9dab4a04db4d40f99bed539198.jpg
0c/0cf6ebf78074e748ab2aeea3a0fcb9e0dd43040974c66e24fa46703f.jpg
5d/5dcfed2e033c2da62e7ac89367533ebbc344373c46a005e274a16785.png
18/18b1f0b13bccb594c6daf296c1f9b6dbd83783bb5ae63fe1716c9091.jpg
35/35bf6095212da882f5d2122fd547800ed993c58956ec39b5a2d52ad4.jpg
-While I am able to display the image in the page, the whole page background becomes black with the image in the middle of the page. (i.e. style="margin: 0px; background: #0e0e0e;>").
What I am trying to do is to insert the image in an HTML tag, so that I can place it somewhere else in the page.
I tried just putting the normal cdn link with 'mangacoverid' attached to it in an tag but the image provider doesn't allow hotlinking and it throws 403 error.
Any help really appreciated!
I tested your code and it seems to be working correctly when the cover ID is hard coded.
Could the issue be that the JSON passed via query param is not getting parsed correctly (due to URL encoding)?
Does it work correctly for you with a cover ID hard coded?
File image.php
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, sprintf('https://cdn.mangaeden.com/mangasimg/%s', $_GET['id']));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$picture = curl_exec($ch);
curl_close($ch);
header('Content-type: image/jpeg');
echo $picture;
And in your script that generates the page displaying the image:
// Assuming $info holds the decoded json for the manga
echo(sprintf('<img src="image.php?id=%s>', $info['im']);
Ok, I'll post the solution in case anyone ever bumps into this.
The code is the mostly the same as original: just edited a few lines as shown below.
-I created a file in the directory called 'tempcover.jpg'.
-I got the image using the function 'imagecreatefromstring($picture)'.
-I saved the image into the file with 'imagejpeg($img,'tempcover.jpg', 100)'.
By doing this I can just refer to the file in the HTML tag and display it the way I want to.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://cdn.mangaeden.com/mangasimg/" . $mangacoverid); //link of API i'm using and adding cover id to it
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$picture = curl_exec($ch);
curl_close($ch);
$img = imagecreatefromstring($picture);
imagejpeg($img,'tempcover.jpg', 100);
?>
<!--HTML-->
<img src="tempcover.jpg" alt="bruh" height="400px" width="300px">

PHP copy() doesn't work for random URLS which redirect to files

trying to copy() .MP3 file from remote url but it always fails.
$link = str_replace(' ','%20','http://mp3hungama.com/music/download.php?song_id=80522');
if (!copy($link,'/home2/muser/tmp/newname.mp3')) {
echo 'copy failed !';
}
$link url redirects to http://mp3hungama.com/music/audio//Indian%20Movies/Indian%20Movies%20Hindi%20Mp3%20Songs/Singh%20Is%20Bling%20(2015)/songs/Cinema%20Dekhe%20Mamma%20#%20Mp3HunGama.Com.mp3
same code works for others random urls like www.example.com/download.php?id=2332. what's the specifically problem here or any other way to do this job ?
I've tested your code and I also couldn't download the file, then, I've used curl an it work as expected:
$local_file = "/home2/muser/tmp/newname.mp3";//This is the file where we save the information
$remote_file = "http://mp3hungama.com/music/download.php?song_id=80522"; //Here is the file we are downloading
$ch = curl_init();
$fp = fopen ($local_file, 'w+');
$ch = curl_init($remote_file);
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_ENCODING, "");
curl_exec($ch);
curl_close($ch);
fclose($fp);
NOTE:
Make sure /home2/muser/tmp/ has write permissions.
TIP:
In the future, if you need to encode/decode a url, use urlencode or urldecode instead of str_replace
This link
already redirects to second link. So it's working already.

Unable to upload file using curl

I need upload a file using php. I have the following code that I am using
<?php
$file = realpath('hello_world.jpg');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://api.newocr.com/v1/upload?key=*My key*');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('file' => '#'.$file));
$result = curl_exec($ch);
echo $result;
curl_close ($ch);
?>
On executing I get the error msg
{"status":"error","message":"File must be uploaded.
https://www.newocr.com/api/"}
But when I manually make a form and upload the image using multipart it works fine. Is something wrong with my code or the issue is with the API
Executing it from command line like this
curl -X POST -F "file=#hello_world.jpg" http://api.newocr.com/v1/upload?key=*my api key*
Works fine
curl_setopt($ch, CURLOPT_UPLOAD, TRUE);
Now, it is giving:
404 Page Not Found
The page you are looking for could not be found. Check the address bar to ensure your URL is spelled correctly. If all else fails, you can visit our home page at the link below.
Visit the Home Page
I had the same problem.
Solution:
<?php
$result = exec('curl -H "Expect:" -F file=#'.realpath($file).' http://api.newocr.com/v1/upload?key=KEY');
$result = json_decode($result, true);
return $result;
?>

Collecting file with PHP CURL after validating request downloads an empty file

I am doing a system where one of my sites goes to the other to get documents.
On the first site I am using Curl to make a request to get the file wanted:
I am using the solution from Download file from URL using CURL :
function collect_file($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, false);
curl_setopt($ch, CURLOPT_REFERER, "http://example.com");
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
return($result);
}
function write_to_file($text,$new_filename){
$fp = fopen($new_filename, 'w');
fwrite($fp, $text);
fclose($fp);
}
$curlUrl = 'http://site2.com/file-depository/14R4NP8JkoIHwIyjnexSUmyJibdpHs5ZpFs3NLFCxcs54kNhHj';
$new_file_name = "testfile-new.png";
$temp_file_contents = collect_file($curlUrl);
write_to_file($temp_file_contents,$new_file_name);
I am testing downloading an image. If i use a direct URL into $curlUrl , for instance http://site2.com/file-depository/image.png it works perfect.
What I am doing is that the URL http://site2.com/file-depository/14R4NP8JkoIHwIyjnexSUmyJibdpHs5ZpFs3NLFCxcs54kNhHj is then parsed and checked against a database to match the document requested, once there is a document matched I need to provide this document to the Curl response.
I have tried many ways to read the file but everytime i am getting a file on the other end but it is only 1kb in size (45 expected) and when trying to open it i get an error unkown file type etc.
On the second site, once the URL is validated here is what I have:
$file = readfile('some-image.png');
echo $file;
I am guessing there is part of the information which belongs to the file missing but can't figure it out, any pointers appreciated!
I have replaced
function write_to_file($text,$new_filename){
$fp = fopen($new_filename, 'w');
fwrite($fp, $text);
fclose($fp);
}
by file_put_contents($new_file_name,trim($temp_file_contents));
Please note the trim(), the issue was that I was apparently collecting some empty space in front of the file content.

pass a variable to curl from get request php

<?php
#$test = $GET['link'];
$url = 'http://cf3.fancyimgs.com/310/20130521/367302239598940361_92b5e3190b3f.jpg';
$ch = curl_init($test);
$fc = fopen('c.jpg', 'w+');
curl_setopt($ch, CURLOPT_FILE, $fc);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fc);
?>
this is not working in codeigniter, above is my php code to get image from an url that i passed through GET
and am not getting the image from the get request url
can anyone help me to solve this.....
and below is the link from where i send the image url
click to download
It should be
$_GET['link']
^
|--- Note the underline
Not
$GET['link']
And
echo urlencode($url) // would be better approach ...

Categories