0

I am trying to access a web service that returns a jpeg image. It requires two variables, time_sent and user_id, in the URL## Heading ##.

Here is my code:

   $ch = curl_init();
   $options =  array(

            CURLOPT_URL => 'www.example.com/service/?time_sent=time&user_id=user',
            CURLOPT_HTTPHEADER => array('Authorization-Token:' . $api_key, 'Content-type: image/jpeg'),
            CURLOPT_ENCODING => "",
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_HTTPGET => true,
            CURLOPT_CONNECTTIMEOUT => 60,
            CURLOPT_TIMEOUT => 60

        );
    }
    curl_setopt_array($ch, $options);
    $response = curl_exec($ch);

    if(!curl_errno($ch)){
        curl_close($ch);
        $img = imagecreatefromstring($response);
        imagejpeg($img);
        imagedestroy($img);
    }
    else{
        curl_close($ch);
        return curl_error($ch);
    }

Here is my output:

ÿØÿàJFIFÿþ>CREATOR: gd-jpeg v1.0 (using IJG JPEG v80), default quality ÿÛC $.' ",#(7),01444'9=82<.342ÿÛC
2!!22222222222222222222222222222222222222222222222222ÿÀÈ•"ÿÄ
ÿĵ}!1AQa"q2‘¡#B±ÁRÑð$3br‚
%&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyzƒ„…†‡ˆ‰Š’“”•–—˜™š¢£¤¥¦§¨©ª²³´µ¶·¸¹ºÂÃÄÅÆÇÈÉÊÒÓÔÕÖרÙÚáâãäåæçèéêñòóôõö÷øùúÿÄ
ÿĵw!1AQaq"2B‘¡±Á #3RðbrÑ $4á%ñ&'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz‚ƒ„…†‡ˆ‰Š’“”•–—˜™š¢£¤¥¦§¨©ª²³´µ¶·¸¹ºÂÃÄÅÆÇÈÉÊÒÓÔÕÖרÙÚâãäåæçèéêòóôõö÷øùúÿÚ?ê¡ñ…n¦TþÌ)Ÿö« ¾Òü1g¤ýºâ5† ¼7ñW5¢xVÑõhXïØ¿6Ö¦|Wß}¤,&¼øÆ2‰´£.b&^Äè÷s)ùê“xkáÕÔª¯ª\¯÷~uÿâk—³ðóN¬îQ*Ëxq¢]ÂUoø (rÈèWÁ~R˹:ý^³ï<áe•Z¾æþÕ’Ú[$Lå¾ía\Þ*3)o—ýšÖ<Ò(éæð­ŒåêˆÊ+VòþÑ4VÓ„ß2ÿyŒ×›ÔÊû¿Þ¨µ%PÊìÌÕ­Œ§Ìu­u ªíß¿o÷k2æõ.%ûì«þõs¯xïº ¶ê9b(È»s>ÖÜ›æ©lüAqoÕÙ«(¾æçîÔEUºSäˆæŸâ¿ò¼Õº’¤¨®Ž¬µåÙ¾]Õ¥i¬ÜÙºì—å_á¨äÑÓk3|ÕËx·^¢ujþ‹­Å~»ä—ø¿Ú¬Ýy[ûA?º”cÊ8™òîÿj˜?Öí©vî¦lÚû«Qšqí1šŠ‰±E2¡4H•õm«÷k–ø†ÒÞø¢ÊÍÓ÷®êêtiâK®_/Þ¬]7^+ó6+Ä©÷·W$yyKŸÅÌe$B$Ø‹M¹e··i–²µmQ–gŠÙÕ]k*Ù5=YÒßÍÜÎßuj£b¹½ÒY®.õyÚÒÆ•Ÿø–¦¶ø_¬N­-ʲ(þõïxfÓF²‹÷éylt­ö@ªrVð‡,N~{ÈùwXðãØM·gûß5bM¦²¯ÿe^ãâû+ifw ªª¿7ûÕæšŒVêÿ#n¨æ÷¹#ÊqO§ÿµP2íêµÐÏ2ðµ›-»V„Jizvê±5¿Ë÷j©ù~SAŸ("í}Õ,Ëòn¦V½SLÉ䪊`Coq-¼©,mµ–¶ŸWûz'™þµWkµXiÑJÈô´­¹TÕbæÊæÞ(¦’DºÍVô;xµ$µ··\<¿:ÿ³]Ĩ~Ýi...etc

Can anyone tell me what I am doing wrong?

OneSneakyMofo
  • 1,305
  • 2
  • 19
  • 33
  • where is defined the var $result .? – MatRt Feb 28 '13 at 21:41
  • `Content-type` header is sent by the response, the requests needs a `Accept` header. Check http://stackoverflow.com/questions/5661596/do-i-need-a-content-type-for-http-get-requests – CodeZombie Feb 28 '13 at 21:43
  • What you're doing wrong? Well for starters you've not set CURLOPT_BINARYTRANSFER, and given the output you've probably not set the mime type on the response to the client. And is there a reason why you use GD to convert the response to jpeg (when it's already one) – symcbean Feb 28 '13 at 21:55
  • @symcbean, imagejpeg produces my output. BINARYTRANSFER is not needed since I am using returntransfer. I don't think I need the mime-type. It was what provided below, but I have multiple headers going on so I'll have to find a way around that. – OneSneakyMofo Feb 28 '13 at 22:05

1 Answers1

2

Can anyone tell me what I am doing wrong?

Nothing really, that looks like a healthy JPG :) Send a

header("Content-type: image/jpeg");

before doing imagejpeg($img); and it should work.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088