2

I am trying to get an chart image from a remote site.
But the image seemed to be created dynamically from the site when called.
It will return nothing when not logged in.

This is the image URL

<img src="http://fuelbuyer.dtn.com/energy/view/energy/chart.do?width=150&height=120&chartType=0&ts=1352196066175&rackId=446&productId=179&points=8&showExtraLine=True">

I somehow managed to log in using this CODE and tried to display image.
But it is not working.

$ch = curl_init();
$url = 'http://fuelbuyer.dtn.com/energy/common/signin.do?';
$login = 'username=$USER&password=$pass&autoLogin=true&partnerId=0&partnerName=';
curl_setopt($ch, CURLOPT_URL, $url.$login);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch); 

Calling the image URL directly (on browser Addressbar ONLY WHEN LOGGED IN) , will give some code like

" ‰PNG IHDR–xŸ-œGPIDATxÚílÇÇ IHKB-ËuÄØª!Eš°Š !ÈB IÀµ©…œ€ $@S‡8¶ ªÀ”D²œÐbR Æ`˜WjAx;~Äø…ç·ï±;³ýÎk×ûºÙݳ™»Þ_+´Ü÷v÷·3óÿf¾™òqnAa@„ùÂM›6åÔ5}£¡k訚Ži踶¾USž†Nh(_M'5tJC Á™°‚ðN?ÿ¼3>¾=5µîòåêÕô©V¢:‰ê%º+QƒD5õ©Y"›D-Õ*Q›Dí}ê¨S¢.‰º%²Kä蓳­Û¹“߸ÑUPà’ˆ“ˆ—õéôéÓ!rBP¸¡Ÿÿ¼®¨¨F"ïò“"´ÂOŠÐ4?G}=ŽŠ"×ÎgdPòc!† #—[WB} "

but when called like

<img src="http://fuelbuyer.dtn.com/energy/view/energy/chart.do?width=150&height=120&chartType=0&ts=1352196066175&rackId=446&productId=179&points=8&showExtraLine=True">

will give the correct image.

I really dont know what to do next.

How can I log into the site using cURL and execute this line.
<img src="http://fuelbuyer.dtn.com/energy/view/energy/chart.do?width=150&height=120&chartType=0&ts=1352196066175&rackId=446&productId=179&points=8&showExtraLine=True">
The site will be redirected to the home page when successfully logged in.
So, I have to prevent from redirection too.
The session will be terminated after few seconds.

Thanking you in advance,
Eugine P J

I got it working. Refer this comment.
Unable to fetch my schedule data from my schools site. Login with cURL wont work

Community
  • 1
  • 1
Eugine Joseph
  • 1,552
  • 3
  • 18
  • 40
  • There is a reason why the website is trying to prevent you doing that. Also you can search this website, this has been asked before. – hakre Nov 07 '12 at 12:01

3 Answers3

2

You need to let the browser know that it's an image, try adding this to your PHP code:

header("Content-Type: image/png");
$ch = curl_init();
$url = 'http://img842.imageshack.us/img842/7650/pngtransparencydemonstr.png';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
echo $output;
Korikulum
  • 2,529
  • 21
  • 25
  • Still not working. It is returning a borken image. I think the only possibility is to remote log in using cURL and getting image from it. But I don't know how. :( – Eugine Joseph Nov 07 '12 at 09:58
  • The image is created using chart.do (Web-based Java program) in the remote server(http://fuelbuyer.dtn.com). the src(in img) sends parameters to the server and returns some characters. these characters are showed as image. I dont know how its working but If logged in we get the image or a broken one. – Eugine Joseph Nov 07 '12 at 10:10
  • @EugineJoseph Try removing `curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);`, but leave the header. – Korikulum Nov 07 '12 at 10:15
  • @EugineJoseph Make sure that the header function is placed at the top. I've add a working example. – Korikulum Nov 07 '12 at 11:58
  • `it is redirected to login page. we can't take this image directly from the site. its dynamic and is created at the requested time.` – Eugine Joseph Nov 07 '12 at 12:29
  • @EugineJoseph Well in that case maybe two `curl_exec()` calls, first one to log in, second to retrieve image. Just use `curl_setopt()` to change the URL in between calls. – Korikulum Nov 07 '12 at 12:41
2

The image you received with cURL is correct (see, you get a PNG header).

If you want to display it on a page of yours - let's ignore licensing issues here - you need to put your scraping code, above, in a page of its own, e.g. myimage.php.

Then in your HTML code you put

<img src="myimage.php" />

and in the myimage.php, once you have $output, you just output it:

<?php
$ch  = curl_init();
$url = 'http://fuelbuyer.dtn.com/energy/common/signin.do?';
$login = 'username='.$USER.'&password=$pass&autoLogin=true&partnerId=0&partnerName=';
curl_setopt($ch, CURLOPT_URL, $url.$login);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$output = curl_exec($ch);
// $info = curl_getinfo($ch);
curl_close($ch); 

// Let's suppose that $content (what we want to send) is exactly equal to $output
$content = $output;
// If, instead, we have in the output something like <img src="crypto-unique.png" />"
// we will need to parse $output (using XML maybe, or, just this once, a regex)
// and get its URL, then retrieve the image using cURL again, and *this* will be our
// final $content.

// Just output
Header("Content-Type: image/png");
Header("Content-Length: " . strlen($content));
die($content);

// Or if we wanted to manipulate it, e.g. send it as JPEG at 75% quality
$gd = imageCreateFromString($content);
Header('Content-Type: image/jpeg');
ImageJPEG($gd, '', 75);
die();

?>

For the sordid details of more complicated login schemes, see the answer to How can I scrape website content in PHP from a website that requires a cookie login?

Community
  • 1
  • 1
LSerni
  • 55,617
  • 10
  • 65
  • 107
  • @lserni- I tried it. but its not working. (but its not wrong too) I have a doubt. How can we log into the site using URL1 and execute URL2 before the login session destroys. Is it possible? if it does, maybe it will do. can you help me in that. – Eugine Joseph Nov 07 '12 at 11:49
  • I have answered something similar not too long ago, see updated answer for the link. BTW, what does "it is not working but it's not wrong either" *mean*? What happened? :-) – LSerni Nov 08 '12 at 09:50
  • @lserni- confused, I think, a little bit. :) This is my first curl program. and still I don't know how its done. :D Thanks man. – Eugine Joseph Nov 08 '12 at 10:18
  • Is it strlen($output) or strlen($content)? – Miheretab Alemu Nov 11 '14 at 17:25
  • @MiheretabAlemu, it actually was neither. I have corrected the code to explain. Maybe you could also be interested in http://stackoverflow.com/questions/13210140/how-can-i-scrape-website-content-in-php-from-a-website-that-requires-a-cookie-lo (code to run a double cURL request there) – LSerni Nov 11 '14 at 18:22
0

Have you tried an image header?

header("Content-Type: image/png");
header("Content-Disposition: attachment; filename=image.png" );
William Isted
  • 11,641
  • 4
  • 30
  • 45