PHP cURL to Simultaneously Save Image & Get Header Response

a 夏天 提交于 2019-12-11 02:49:14

问题


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.

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.

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.

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.

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.


回答1:


2 hours after...

<?
//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); 

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.



来源:https://stackoverflow.com/questions/29783752/php-curl-to-simultaneously-save-image-get-header-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!