PHP cURL以同时保存图像和& amp;获取标题响应 [英] PHP cURL to Simultaneously Save Image & Get Header Response

查看:85
本文介绍了PHP cURL以同时保存图像和& amp;获取标题响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用cURL同时将图像保存到文件,同时还返回加载该文件时返回的标头进行保存.

I'm trying to use cURL to simultaneously save an image to file while also returning the headers that were returned upon loading that file for saving.

原因是因为每次加载和/或保存图像时都会生成一个唯一的cookie,所以如果我保存文件,然后对同一图像URL进行另一个请求以获取cookie,则该cookie不会与已保存的图像(验证码图像)正确配对.

The reason is because there is a unique cookie that is generated every single time the image is loaded and/or saved, so if I save the file and then do another request to the same image URL to get the cookie, the cookie will not be appropriately paired with the image that was save (it is a captcha image).

该图像只能加载一次,并且必须在该图像的单次加载中保存该图像(,而无需从服务器重新请求图像),并且必须同时保存显示标题,以便获取加载时生成的Cookie.保存图像.

The image can only be loaded once, and on that single load of the image it must be saved (without re-requesting the image from the server) and simultaneously show the headers so I can get the cookie that was generated upon loading & saving the image.

到目前为止,这是我做的,确实返回标头并保存文件,但是当以.jpg格式查看时,文件已损坏.如果我将文件类型更改为.txt,则可以看到标题,但可以看到一堆乱码,这些标题不是标题下的图像.因此很明显,要保存的文件是标题的组合,然后是图像,我只是无法单独获取它们,同时确保对图像的请求只有一个.

This is what I've got so far, which DOES return the header and DOES save a file, but the file is corrupt when viewed as a .jpg. if I change the filetype to .txt, I can see the headers, but then a bunch of garbled characters that are not an image beneath the headers. So it's clear the file that is being saved is a combination of the headers and then what should be an image, I just can't get them separately while making sure there is only one single request for the image.

function getImageandCookie($ImageURL) {
    $rand = rand();
    $image_file = $_SERVER['DOCUMENT_ROOT'] . '/image/' . $GLOBALS['id'] . $rand . '.jpg';

    $fp = fopen ($image_file, 'w+');

    $ch = curl_init($ImageURL);

    curl_setopt($ch, CURLOPT_FILE, $fp);      
    curl_setopt($ch, CURLOPT_HEADER, 1);    
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);      
    curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36');

    $data = curl_exec($ch);

    curl_close($ch);
    fclose($fp); 
    return $data;
}

更多详细信息:我试图将图像保存到服务器上的文件中,同时使加载该图像时返回的标头可用于脚本的其余部分.

More details: I'm trying to save the image to a file on my server while simultaneously making the headers that were returned while loading that image for saving available to the rest of my script.

如果您加载此图像: http://ipv4.google.com/sorry/image?id=2125815723022350864&hl=zh_CN ,您会看到创建了一个绑在一起的Cookie"图片中的文字.如果您重新加载图像或对同一URL提出新请求,则会使用新的Cookie&图片"pair"已创建.

If you load this image: http://ipv4.google.com/sorry/image?id=2125815723022350864&hl=en you'll see that a cookie is created that is "tied" to the text within the image. If you reload the image or make a new request to that same URL, a new cookie & image "pair" are created.

因此,我需要一次加载该图像并将其保存到文件中,同时抓取标头(这是绑定"到该特定图像的cookie所在的位置),同时确保只请求一次图像

So I need to load that image one time and save it to file while simultaneously grabbing the headers (as that is where the cookie that is "tied" to that specific image is) while making sure to only request the image one time.

推荐答案

2小时后...

<?
//error_reporting(E_ALL);
//ini_set('display_errors', '1');

    $image_file = "captcha.jpg";
    //$cookie = "gcookie";

    $ch = curl_init("http://ipv4.google.com/sorry/image?id=2125815723022350864&hl=en");  
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    //curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
    //curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);      
    curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36');
    $data = curl_exec($ch);
    //split the header and body of request
    $matches = preg_split('/^\s*$/im', $data);
    $header = $matches[0];
    //extract cookie from header
    preg_match_all('/Set-Cookie: (.*?)\s+/i', $header, $gCookie, PREG_PATTERN_ORDER);
    $gCookie = $gCookie[1][0];
    echo $gCookie;
//GOOGLE_ABUSE_EXEMPTION=ID=a85908efa22e6f9b:TM=1429660423:C=c:IP=x.x.x.x-:S=APGng0vbHyNi1KCn9O1bnspO8BgF4LFEhQ;

    //The body is the image, we cleanup the header/body line break and save it
    $body = $matches[1] ;
    $body = implode("\n", array_slice(explode("\n", $body), 1));
    file_put_contents($image_file, $body);

curl_close($ch); 

理解了以下事实并不难,当我们将 CURLOPT_HEADER设置为1 时,响应头位于 $ data = curl_exec($ ch); 中,那么,我们只是需要拆分 header body 以在 header 中找到 cookie 并保存 body (图像)到文件.

It wasn't difficult after understanding that when we set CURLOPT_HEADER, 1 the response headers come inside $data = curl_exec($ch);, then, we just need to split the header and body to find the cookie in the header and save the body (image) to a file.

这篇关于PHP cURL以同时保存图像和&amp; amp;获取标题响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆