0

why some url with json content return null on php function get page content?

i used these ways :

  • file_get_content
  • curl
  • http_get

but return null . but when page open with browser json content show on browser ?? anyone can help me?

    $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);

return null but in browser show

{"error_description": "Access token has expired.", "error": "invalid_credentials"}
bnnoor
  • 656
  • 2
  • 13
  • 31

1 Answers1

1

It returns the following: failed to open stream: HTTP request failed! HTTP/1.1 401 UNAUTHORIZED

So, you have to be authorized.

You ca play with this code snippet (I can't test since token expired again):

<?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;
vich
  • 262
  • 2
  • 15
  • how i can ass Authorization to file_get_contents header? – bnnoor Oct 08 '18 at 21:28
  • As Harshad Shettigar wrote, you can't hardcode access_token. So, you have to get actual access_token and replace it in $url. How to get this access_token it's another question and depends from where you copied $url. Maybe you can make separate response to get actual access_token or so. – vich Oct 08 '18 at 21:32
  • i have access_token but i do not know how i can use in header ? – bnnoor Oct 08 '18 at 21:33
  • Try this: `$access_token = "9IaRpdmQzFppJMabLHbHyzBaQnm8bM"; // use your actual token $content = file_get_contents("https://pardakht.cafebazaar.ir/devapi/v2/api/validate/com.pdgroup.insta/inapp/coin_follow_1/purchases/324234235/?access_token=" . $access_token); echo $content;` – vich Oct 08 '18 at 21:35
  • can i use this way? $opts = array('http' => array( 'method' => 'POST', 'header' => ['Content-type: application/x-www-form-urlencoded','Authorization:'.$accessToken] ) ); $context = stream_context_create($opts); – bnnoor Oct 08 '18 at 21:35
  • i used this way but return empty response . – bnnoor Oct 08 '18 at 21:37
  • you can use this url with right access_code but file_get_contents still return null "https://pardakht.cafebazaar.ir/devapi/v2/api/validate/com.pdgroup.insta/inapp/coin_follow_1/purchases/324234235/?access_token=s9spZax2Xrj5g5bFZMJZShbMrEVjIo" – bnnoor Oct 08 '18 at 21:39
  • with new access_token it now returns **failed to open stream: HTTP request failed! HTTP/1.1 404 NOT FOUND** – vich Oct 08 '18 at 21:43
  • but url open in browser ! – bnnoor Oct 08 '18 at 21:44
  • With CURL you can use header Authorization: Basic YWxhZGRpbjpvcGVuc2VzYW1l (https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Authorization) or curl_setopt($process, CURLOPT_USERPWD, $username . ":" . $password); if you have username/password (https://stackoverflow.com/questions/2140419/how-do-i-make-a-request-using-http-basic-authentication-with-php-curl) – vich Oct 08 '18 at 21:45
  • can i use file_get_contents with header like this? array( 'method' => 'POST', 'header' => ['Content-type: application/x-www-form-urlencoded','Authorization: Basic '.$accessToken] ) – bnnoor Oct 08 '18 at 21:48
  • Depends on headers which you sends to pardakht.cafebazaar.ir it could provide different responses. For example provide JSON if you send the following headers $headers = array("Accept: application/json", "Content-Type: application/json"); or so. – vich Oct 08 '18 at 21:48
  • to send some headers you have to use CURL like in link I mentioned several comments above – vich Oct 08 '18 at 21:50
  • can you put sample code in curl? my problem is i cant set access_code in curl header . i see page but i do not know yet! – bnnoor Oct 08 '18 at 21:52
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/181504/discussion-between-vich-and-user3243573). – vich Oct 08 '18 at 22:03