【问题标题】:I'm having difficulty getting a JSON in a URL with PHP. Can you help me?我很难使用 PHP 在 URL 中获取 JSON。你能帮助我吗?
【发布时间】:2021-07-31 13:46:36
【问题描述】:

我正在尝试使用以下代码从 URL 获取 JSON。 我总是收到以下错误。

<?php
$url = ...;
$json = file_get_contents($url);
$dados = json_decode($json, true);
echo "<pre>";
print_r($dados);
?>

错误:

警告:file_get_contents(https://www.car.gov.br/publico/imoveis/getImovel?lat=-2.449376438504347&lng=-49.04754638759188):无法打开流:已达到重定向限制,正在中止 ... 第 5 行

使用的另一个代码:

<?php
$url = ...;
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$url);
$dados = curl_exec($ch);
curl_close($ch);
echo "<pre>";
var_dump(json_decode($dados, true));
?>

回应:

当使用相同的代码时,带有另一个 URL,例如:

$url = 'http://api.ipstack.com/177.194.116.144?access_key=65a2a7545d3ea66448ca270dad2dc789'

反馈正常。

你能帮帮我吗?

问候, 迭戈

【问题讨论】:

  • 这是您使用的两个不同的 URL。根据错误消息,第一个看起来会导致重定向循环。在您的 curl 代码中,您设置了 CURLOPT_HTTPHEADER,它只会返回 HTTP 响应的标头部分。所以你不应该期望那里有一个响应体。

标签: php json curl


【解决方案1】:

以下似乎有效

使用卷曲:

    <?php
    
    $url = 'https://www.car.gov.br/publico/imoveis/getImovel?lat=-2.449376438504347&lng=-49.04754638759188';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:90.0) Gecko/20100101 Firefox/90.0',
    'Accept-Encoding: gzip, deflate',
    'Accept-Language: en-US,en;q=0.5',
    'Accept-Encoding: gzip, deflate, br',
    'Connection: keep-alive',
    'Cookie: PLAY_SESSION=8c1bbeb68ce32508d4f7ef0003aa61712eb9a12c-___ID=6e7f9897-25b8-49bf-9b7b-974d76f0cc7f; BIGipServerpool_car_https=1681002668.47873.0000; TSb0b96d3e027=08f250eeb3ab2000fa851676d8c0aafe8c0ee0dbbae1a0b5d329a962f73f7693776786518e21bc5608aead07e0113000cea9a18e252ae4845188f4c7c7a8432eebbd8d46f8dd8ddadfc873935b69ea08f6280aee6e084adc65f9193780ad34e3; TS570bd070029=08f250eeb3ab2800fe23562db9686a4e0156e1ac772b60e389613e4652d1c435d0008193c7ae5d55de82b5e4b6ad8d23',
    'Upgrade-Insecure-Requests: 1',
    'Sec-Fetch-Dest: document',
    'Sec-Fetch-Mode: navigate',
    'Sec-Fetch-Site: none',
    'Sec-Fetch-User: ?1',
    'Pragma: no-cache',
    'Cache-Control: no-cache',    
    ));
    

curl_setopt($ch, CURLOPT_VERBOSE, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$dados = curl_exec($ch);
$error = curl_error($ch);
    
    ?>

回复:

{"type":"FeatureCollection","crs":{"type":"name","properties":{"name":null}},"features":[{"type":"Feature","geometry":{"type":"MultiPolygon","coordinates":[[[[-49.0527,-2.4519],[-49.0533,-2.4551],[-49.0373,-2.4524],[-49.0367,-2.445],[-49.0517,-2.4478],[-49.0527,-2.4519]]]]},"properties":{"id":"ptS9jr7pPn+kAaDdfHlORQ==","codigo":"PA-1504703-73C3A10E7D0A4E1D8E207BD30890C7CD","area":137.3599,"status":"AT","tipo":"IRU","municipio":"Moju","categoria":"IMOVEL"},"id":"ptS9jr7pPn+kAaDdfHlORQ=="}]}

如果没有正确的标头,服务器会以空正文响应。因此,请确保在发出请求之前设置它们。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-21
    • 1970-01-01
    • 1970-01-01
    • 2021-01-28
    • 1970-01-01
    • 1970-01-01
    • 2017-12-19
    • 2017-12-23
    相关资源
    最近更新 更多