-5

i have been stuck on this issue for days now. i have this json file under 'https://prices.csgotrader.app/latest/prices_v6.json' but when you open the link in the browser, it is prompted to download and not to inspect. So in PHP its unable to inspect the details. I had found a way around this to upload the file on my CPanel and this then allowed me to inspect teh details using the url 'ryzen.me/prices.json'. One problem with this is that the original json is updated on a daily basis and it would not be a viable option for me to download it and upload it manually daily.

How do i turn this url readable for my php to be able to inspect and use the information?

ADyson
  • 57,178
  • 14
  • 51
  • 63
Zaiga
  • 21
  • 5
  • downloading it in a browser goes nowhere near PHP, because php runs on your server. You can have your PHP code download the file using cURL or file_get_contents, and process it. If you want to automatically download it daily, then maybe use a background script executed via cron. – ADyson Sep 16 '22 at 16:13
  • the issue is its not that php cant read the download, its that its not json – Lawrence Cherone Sep 16 '22 at 16:13
  • I downloaded the file and copied/pasted the contents into a JSON validator and it passed. However, `file_get_contents()` seems to return binary. Not 100% but I feel like there's a server-side header that's forcing clients to try and save the file as a file – outlaw Sep 16 '22 at 16:17
  • Not sure what you mean? A file is a file, whether it's binary or text. But file_get_contents doesn't automatically do anything with any response headers it receives, and it also puts the response body data into a PHP variable, not a file - it's then up to you to write your own code if you want to put it into a file. It might be an idea if you show us the PHP code you've actually written for this, and how you're viewing its output. – ADyson Sep 16 '22 at 16:19
  • @LawrenceCherone jsonlint says it's valid JSON. Is there something specific which led you to that remark? – ADyson Sep 16 '22 at 16:20
  • @ADyson aren't these headers forcing the client to recognize the server's response as binaray? `< Content-Type: binary/octet-stream < Content-Length: 1141960 < Content-Encoding: gzip` – outlaw Sep 16 '22 at 16:25
  • Possibly related https://stackoverflow.com/questions/6293893/how-do-i-force-files-to-open-in-the-browser-instead-of-downloading-pdf – j08691 Sep 16 '22 at 16:25
  • @outlaw they don't _force_ anything. It's up to the client whether it takes any notice of the response headers. – ADyson Sep 16 '22 at 16:26
  • @ADyson did you try with CURL as I posted? – Rakesh Mehta Sep 16 '22 at 17:14
  • @RakeshMehta No. But then it wasn't me who asked the question. – ADyson Sep 16 '22 at 17:23
  • Oops sorry @ADyson, my mistake. – Rakesh Mehta Sep 16 '22 at 17:30

1 Answers1

-1

Try using CURL instead..

<?php

$url = "https://prices.csgotrader.app/latest/prices_v6.json";

$options = [
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_HEADER => false,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_ENCODING => "",
  CURLOPT_USERAGENT => "CURL",
  CURLOPT_AUTOREFERER => true,
  CURLOPT_CONNECTTIMEOUT => 120,
  CURLOPT_TIMEOUT => 120,
  CURLOPT_MAXREDIRS => 10
];

$ch = curl_init ($url);
curl_setopt_array ( $ch, $options );

$return = [];
$return['response'] = curl_exec ( $ch );
$return['errno'] = curl_errno ( $ch );
$return['errmsg'] = curl_error ( $ch );
$return['httpcode'] = curl_getinfo ( $ch, CURLINFO_HTTP_CODE );

curl_close ($ch);

if($return['httpcode'] == 200)
{
    echo $return['response']; //Here is your response

}

?>

Here is a working example https://paiza.io/projects/e/rMwG3pOC6sRdA89gU885ag

Rakesh Mehta
  • 519
  • 2
  • 9