简体   繁体   中英

Send GET request to MapQuest using PHP cURL

I'm trying to get a map using MapQuest using this URL

https://www.mapquestapi.com/staticmap/v5/map?key={key here}&center=30.047565,31.243452&size=@2x&zoom=14&type=light&locations=30.047565,31.243452&defaultMarker=marker-sm-3B5998-22407F

according to documentation the result is an image

https://developer.mapquest.com/documentation/static-map-api/v5/map/

when I use POSTMAN I get an image (no json data to parse) and when I use this code as an image the map sometimes doesn't show up

 <img style="width: 100%; height: 450px; overflow: hidden" src="https://www.mapquestapi.com/staticmap/v5/map?key={key here}&center=30.047565,31.243452&size=1280,400@2x&zoom=18&type=light&locations=30.047565,31.243452&defaultMarker=marker-sm-3B5998-22407F">

so I'm trying to get it using PHP cUrl

curl_setopt_array($curl, [
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_URL => 'https://www.mapquestapi.com/staticmap/v5/map?key={key here}&center=30.047565,31.243452&size=1280,400@2x&zoom=18&type=light&locations=30.047565,31.243452&defaultMarker=marker-sm-3B5998-22407F',
        CURLOPT_USERAGENT => 'Codular Sample cURL Request'
    ]);

    $resp = curl_exec($curl);

    curl_close($curl);
    print_r($resp); 

But I get something like bitmap or image code how can I display this as an image?

在此处输入图片说明

You need to have the headers that their response gives you, specifically the Content-Type header.

   function handle_headers($curl, $header_line) {
      //this will only send their "Content" headers, you will have to pick and choose which headers you want
      if(substr($header_line, 0, 8) == 'Content-')
          header($header_line);
   }

   curl_setopt_array($curl, [
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_HEADERFUNCTION => 'handle_headers',
        CURLOPT_URL => 'https://www.mapquestapi.com/staticmap/v5/map?key={key here}&center=30.047565,31.243452&size=1280,400@2x&zoom=18&type=light&locations=30.047565,31.243452&defaultMarker=marker-sm-3B5998-22407F',
        CURLOPT_USERAGENT => 'Codular Sample cURL Request'
    ]);

    $resp = curl_exec($curl);

    curl_close($curl);
    echo $resp;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM