Robert Jaskowski
Robert Jaskowski

Reputation: 823

Get image via https and php

Hy!

I'm trying to get an image per https in php.

What I've found is something like this code which shows me an blank image with correct width but wrong height:

    function getSslPage($url) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_REFERER, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }

    echo getSslPage("https://www...");

I'm grateful for any help. :)

Upvotes: 0

Views: 2185

Answers (2)

FMK Berlin
FMK Berlin

Reputation: 21

This should do it in any situation:

$ch = curl_init ($source_url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,true);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$rawdata=curl_exec($ch);
curl_close ($ch);

$fp = fopen($local_file,'w');
fwrite($fp, $rawdata);
fclose($fp)

Upvotes: 2

Yatin Trivedi
Yatin Trivedi

Reputation: 669

It should work,

try to run this code,

function getSslPage($url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_REFERER, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($ch);
$ctype="image/png";
header('Content-type: ' . $ctype);
    curl_close($ch);
    return $result;
}

echo getSslPage("https://ssl.gstatic.com/accounts/services/mail/phone.png");

Upvotes: 0

Related Questions