我正试图从远程站点获取图表图像 但是图像似乎是在调用时从网站动态创建的 未登录时,它将不返回任何内容。
这是图片网址
<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">
我以某种方式设法使用此代码登录并尝试显示图像 但它没有用。
$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);
直接调用图像URL(仅在登录时在浏览器地址栏上),将提供一些代码,如
” ‰PNG IHDR-XY-œGPIDATxÚílÇÇIHKB-ËuÄتES°S EBIÀμ©...œ€$ @小号‡8¶ªÀ”D²œÐbRÆ`~WjAx;!〜AO ...... C·I±;³ýÎk×ûºÙݳ™»Þ_+ 'U÷v÷·3óÿf¾™òqnAa@“UAM>6åÔ5}£¡k訚Ži踶¾USž†NH(_M'5tJCÁ™°,DNÿ¼3&GT; 1 H =5μîòåêÕô©v键¢:‰ê%º+QƒD5õ ©Y“> d-O * Q>迪} ES¢。‰º%²Kä蓳۹“߸ÑUPà'“-õéôéÓ!rBP¸¡Ÿÿ¼®¨¨F”IO“,“'ÂOŠÐ4?g } =ŽŠ“×ÎgdPòc!†# - [WB} “
但是当被称为
时<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">
会给出正确的图像。
我真的不知道接下来该做什么。
如何使用cURL登录网站并执行此行
<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">
成功登录后,该站点将重定向到主页
所以,我也要防止重定向。
会话将在几秒钟后终止。
提前感谢你,
Eugine P J
我得到了它的工作。请参阅此评论 Unable to fetch my schedule data from my schools site. Login with cURL wont work
答案 0 :(得分:2)
您需要让浏览器知道它是一个图像,尝试将其添加到您的PHP代码中:
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;
答案 1 :(得分:2)
您使用cURL收到的图片是正确的(请参阅,您将获得一个PNG标题)。
如果您想在自己的页面上显示它 - 让我们忽略许可问题 - 您需要将您的抓取代码(上面的)放在自己的页面中,例如: myimage.php
。
然后在您的HTML代码中输入
<img src="myimage.php" />
并在myimage.php
中,一旦你有$output
,就输出它:
<?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();
?>
有关更复杂的登录方案的肮脏细节,请参阅How can I scrape website content in PHP from a website that requires a cookie login?
的答案答案 2 :(得分:0)
您是否尝试过图像标题?
header("Content-Type: image/png");
header("Content-Disposition: attachment; filename=image.png" );