【问题标题】:Send GET request to MapQuest using PHP cURL使用 PHP cURL 向 MapQuest 发送 GET 请求
【发布时间】:2019-07-26 16:02:43
【问题描述】:

我正在尝试使用此 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

根据文档,结果是图像

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

当我使用 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">

所以我正在尝试使用 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); 

但是我得到了位图或图像代码之类的东西,如何将其显示为图像?

【问题讨论】:

    标签: php curl get mapquest


    【解决方案1】:

    您需要拥有他们的响应提供给您的标头,特别是 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;
    

    【讨论】:

      猜你喜欢
      • 2018-03-30
      • 2012-08-31
      • 1970-01-01
      • 2016-02-22
      • 2013-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多