file_get_content возвращает null для некоторого URL - PullRequest
0 голосов
/ 09 октября 2018

почему некоторые URL с содержимым json возвращают ноль в php-функции для получения содержимого страницы?

я использовал эти способы:

  • file_get_content
  • curl
  • http_get

, но вернуть ноль.но когда страница открывается с помощью браузера JSON содержание показать в браузере?кто-нибудь может мне помочь?

    $url = 'https://pardakht.cafebazaar.ir/devapi/v2/api/validate/com.pdgroup.insta/inapp/coin_follow_1/purchases/324234235/?access_token=9IaRpdmQzFppJMabLHbHyzBaQnm8bM';
$result = file_get_content($url);

вернуть ноль, но в браузере показать

{"error_description": "Access token has expired.", "error": "invalid_credentials"}

1 Ответ

0 голосов
/ 09 октября 2018

Возвращает следующее: не удалось открыть поток: ошибка HTTP-запроса!HTTP / 1.1 401 UNAUTHORIZED

Итак, вы должны быть авторизованы.

Вы можете поиграть с этим фрагментом кода (я не могу проверить, так как токен снова истек):

<?php
$access_token = "s9spZax2Xrj5g5bFZMJZShbMrEVjIo"; // use your actual token
$url = "https://pardakht.cafebazaar.ir/devapi/v2/api/validate/com.pdgroup.insta/inapp/coin_follow_1/purchases/324234235/";

// Solution with file_get_contents
// $content = file_get_contents($url . "?access_token=" . $access_token);
// echo $content;

// Solution with CURL
$headers = array(
    "Accept: application/json",
    "Content-Type: application/json",
    "Authorization: Basic " . $access_token
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $url); //  . $params - if you need some params
curl_setopt($ch, CURLOPT_POST, false);
// curl_setopt($ch, CURLOPT_USERPWD, $userPwd);

$result = curl_exec($ch);
$ch_error = curl_error($ch);
$info = curl_getinfo($ch);

curl_close($ch);

if ($ch_error) {
    throw new Exception("cURL Error: " . $ch_error);
}

if ($info["http_code"] !== 200) {
    throw new Exception("HTTP/1.1 " . $info["http_code"]);
}

echo $result;
...