【问题标题】:Proxy blocking file_get_contents代理阻止 file_get_contents
【发布时间】:2013-09-13 10:29:38
【问题描述】:

在我的业务中,我必须在我的应用程序中使用谷歌地图(计算距离) 我们目前使用代理的配置脚本。 在我的应用程序中,我使用该方法查询file_get_contents Google Map。

$url = 'http://maps.google.com/maps/api/directions/xml?language=fr&origin='.$adresse1.'&destination='.$adresse2.'&sensor=false';;

  $xml=file_get_contents($url);
  $root = simplexml_load_string($xml);
  $distance=$root->route->leg->distance->value;
  $duree=$root->route->leg->duration->value; 
  $etapes=$root->route->leg->step;
  return array(
     'distanceEnMetres'=>$distance,
     'dureeEnSecondes'=>$duree,
     'etapes'=>$etapes,
     'adresseDepart'=>$root->route->leg->start_address,
     'adresseArrivee'=>$root->route->leg->end_address
  );
}

但是使用代理我有一个未知的主机错误。 (我测试了我的家,代码工作正常)。我想知道是否有办法考虑我在浏览网页时标识自己的代理?

【问题讨论】:

    标签: php proxy file-get-contents


    【解决方案1】:

    您可以使用cURL 执行此操作。它比简单地调用file_get_contents() 更冗长,但更可配置:

    $url = 'http://maps.google.com/maps/api/directions/xml?language=fr&origin='.$adresse1.'&destination='.$adresse2.'&sensor=false';
    
    $handle = curl_init($url);
    curl_setopt($handle, CURLOPT_PROXY, ''); // your proxy address (and optional :port)
    curl_setopt($handle, CURLOPT_PROXYUSERPWD, ''); // credentials in username:password format (if required)
    curl_setopt($handle, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);
    $xml = curl_exec($handle);
    curl_close($handle);
    
    //continue logic using $xml as before
    

    【讨论】:

      【解决方案2】:

      google 对其 api 的每个时间段的查询数量有限制

      首先你必须在你身边缓存结果,并在查询之间添加暂停

      https://developers.google.com/maps/documentation/business/articles/usage_limits

      【讨论】:

        【解决方案3】:

        当然,您也可以提及“上下文”以使 file_get_contents 识别代理。请查看我自己的问题和我自己对该问题的回答here

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-11-17
          • 2019-05-02
          • 1970-01-01
          • 2013-10-21
          • 2022-06-11
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多