Problems with the result Curl and file_get_contents

Asked

Viewed 545 times

4

I’m trying to get an image from a website to use imagecreatefrompng(), but the backlash was never what I expected...

Url: https://www.habbo.com.br/habbo-imaging/avatarimage?user=vfr&direction=3&size=m in the case when I call as <img> works perfectly

<img src="https://www.habbo.com.br/habbo-imaging/avatarimage?user=vfr&direction=3&size=m">

the problem is that when I call Curl returns it:

Gif89a6> XXXXLLL555AAAFFF))//_t#4 ه* íl Q Ђ! (c) sulake! ,6> H * Ȱ Ç#J H ŋ3j ȱ Ǐ C I ɓ(S J -c �ʘ��I��͓(P�@ѣpI2���D�"M�Td˧�5ʵ(��?=� Ukׯh�vp�@۶e�"E@�n،l���@ݝ<�^d�voٿ�w�L����"HLy�b�� ?� [9q�� j~�����:AӤ��qD�o9���Z&��!� >p�3m�(;3�CؓoS�s��3�H8�e��[.����t����߾�]�s�������4[ ��z#5 @k _L < ��7�~��t*aKV nx }! # h B b . 0~@8 H! Jėh>J ��/ӎ�g�&�4a�� )�7�!� /�$�Ӆ)�Kh���l���p�)�tB;

already tried to use file_get_contents but it’s a https url and it doesn’t work, I want you to return the image to use with imagecreatefrompng() from php

code I used:

function getimg($url) {
    $headers[] = 'Accept: image/gif, image/x-bitmap, image/jpeg, image/pjpeg';
    $headers[] = 'Connection: Keep-Alive';
    $headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8';

    $process = curl_init($url);

    curl_setopt($process, CURLOPT_URL, $url);
    curl_setopt($process, CURLOPT_HEADER, false);
    curl_setopt($process, CURLOPT_HTTPHEADER, array('Accept-Encoding: gzip, deflate, sdch'));
    curl_setopt($process, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    curl_setopt($process, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($process, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($process, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($process, CURLOPT_SSL_VERIFYPEER, 0);

    $return = curl_exec($process);
    curl_close($process);

    return $return;
}

echo getimg('https://www.habbo.com.br/habbo-imaging/avatarimage?user=vfr&direction=3&size=m');

//echo '<img src="'.getimg('https://www.habbo.com.br/habbo-imaging/avatarimage?user=vfr&direction=3&size=m').'">';
//$habbo = imagecreatefrompng('https://www.habbo.com.br/habbo-imaging/avatarimage?user=vfr&direction=3&size=m');
//imagecopy($im, $habbo, -2, -9, 0, 0, 33, 33);
  • Step face be wrong, but the Content-Type:shouldn’t be image/jpeg ?

  • Oh... it seems that if you qualify in your php.ini the extension=php_openssl.dll and allow_url_fopen = On, you can use file_get_content... I don’t know, just ideas...

  • Tried something like header('Content-type: image/png'); for example, to know if the image is mounted? The $process = curl_init($url); can be $process = curl_init(); in your case, since you set the url using the CURLOPT_URL.

2 answers

3


I just tested here successfullyinserir a descrição da imagem aqui:

It can actually be simpler than what you were doing:

function getimg($url) {
    $process = curl_init();
    curl_setopt($process, CURLOPT_URL, $url);
    curl_setopt($process, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($process, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    $return = curl_exec($process);
    curl_close($process) ;
    return $return;
}

header("Content-Type: image/jpeg");
echo getimg('https://www.habbo.com.br/habbo-imaging/avatarimage?user=vfr&direction=3&size=m');

Here a solution with file_get_contents():

function getimg($url) {
    $options = array(
        'http'=>array(
            'method'=> "GET",
            'header'=> "Accept-language: en\r\n" .
            'User-Agent: ' .$_SERVER['HTTP_USER_AGENT']. "\r\n"
        )
    );
    $context = stream_context_create($options);
    return file_get_contents($url, false, $context);
}
header("Content-Type: image/jpeg");
echo getimg('https://www.habbo.com.br/habbo-imaging/avatarimage?user=vfr&direction=3&size=m');
  • I appreciate the help! Curl did not return anything, but file_get_contents() worked properly. Thanks!

  • Curl did not return? Strange, as you can see by the image resulted in my also @Leandroferreira

  • http://image.prntscr.com/image/1cdabad1221c412aa873b10731c24990.png also found strange @Miguel

  • @Leandroferreira, try taking the header('content....) see if you see any errors sff, or try adding curl_setopt($process, CURLOPT_HEADER, false); as I have in the image

  • I tried both alternatives, both did not return, tried to change the header (gif,jpeg,png...), last one or die and got this return http://image.prntscr.com/image/f0be091e9b134a0b8dd39f7bd4ef06d0.png, @Miguel

  • I have no idea what it might be, but if you take out the "die()" it does not return any error? Have the errors enabled @Leandroferreira

  • I will test on other servers, how it works in yours, I have no doubt that the code is functional. http://image.prntscr.com/image/718e151b55ab4afb8e9a543c0a163fad.png @Miguel

  • ): however if you find out what it was comes here to say sff @Leandroferreira , but good that the file_get_contents() results

  • Okay. If I find out I’ll come back. I appreciate the help!

Show 4 more comments

1

You can use the following code, which makes use of the file_get_contents instead of curl, in it is passed the User-Agent so that the connection is not rejected, the capture of the mime-type automatically via the function finfo.

To avoid errors in obtaining the mime-type, check on php.ini if the dll of fileinfo is enabled extension=php_fileinfo.dll without the ;.

function getimg($url) {
$options = array(
  'http'=>array(
    'method'=>"GET",
    'header'=>"User-Agent: Mozilla/5.0 (Android; Mobile; rv:40.0) Gecko/40.0 Firefox/40.0
\r\n" // Edite com o user agent que você deseja usar
  )
);

$context = stream_context_create($options);
$image = file_get_contents($url, false, $context);

//Obtem o Mime Type
$file_info = new finfo(FILEINFO_MIME_TYPE);
$mime_type = $file_info->buffer($image);

return 'data:'.$mime_type.';base64,'.base64_encode($image);
} 

echo '<img src="'.getimg('https://www.habbo.com.br/habbo-imaging/avatarimage?user=vfr&direction=3&size=m').'">';

With this you can make use of the <img src= to use the images directly in the code without the need to use the header.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.