简体   繁体   English

使用PHP cURL将GET请求发送到MapQuest

[英]Send GET request to MapQuest using PHP cURL

I'm trying to get a map using MapQuest using this URL 我正在尝试通过此网址使用MapQuest获取地图

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 当我使用POSTMAN时,我得到一个图像(没有要解析的json数据),当我将此代码用作图像时,地图有时不显示

 <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 所以我正在尝试使用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. 您需要具有其响应提供的标头,尤其是Content-Type标头。

   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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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