PHP cURL同时保存图像&获取头部响应 [英] PHP cURL to Simultaneously Save Image & Get Header Response

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

问题描述

我试图使用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,因此如果我保存文件,然后对同一图像网址进行另一个请求以获取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).

图片只能加载一次图像,它必须被保存(不需要从服务器重新请求图像),并同时显示标题,这样我可以获得在加载&保存图像。

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-TW ,您会看到系统建立了一个「连结」到文字内的Cookie图片。如果您重新加载图片或向相同的网址发出新的请求,图像对。

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, / code>,然后,我们只需要将 $ data = curl_exec($ ch); / code>和 body ,执行 preg_match ,找到 code>并清除并将正文(image)保存到文件

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, do a preg_match to find the cookie in the header and cleanup and save the body (image) to a file.

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

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