Json to Array with the API's

Discuss all non-Minecraft gaming related topics here
User avatar
EmberCelica
Engineer
Engineer
CPU: i7 6700k
RAM: 16GB DDR4
Motherboard: [Unknown]
GPU: AMD RX480
Cooling: Fan Cooling
Location: Always behind you
Posts: 188
Joined: Sat Feb 28, 2015 11:20 pm

Json to Array with the API's

Sun Nov 13, 2016 9:06 pm

Pri, thankfully, released an API for us who want to code a script to monitor or record different information from the site. These are structured in the form of a JSON, which every programming language has a library for. That being said, i decided to approach this with PHP, using curl. After some time, i have a base code together for anyone to use, just replace the

Code: Select all

//cookie
with your cookie:

Code: Select all

<?php
 
  // CURL function for processing all the services simultaneously
  function CurlURL($Curl_This_URL) {
 
    $ch = curl_init("$Curl_This_URL");
   
    $options = array(
      CURLOPT_CONNECTTIMEOUT => 5,
      CURLOPT_TIMEOUT => 5,
      CURLOPT_AUTOREFERER => true,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_SSL_VERIFYPEER => 0 ,
      CURLOPT_SSL_VERIFYHOST => 0 ,
	  CURLOPT_HTTPHEADER => array(//cookie)
    );
 
    curl_setopt_array($ch, $options);
    $curl_page_result = curl_exec($ch);
    curl_close($ch);
   
    return $curl_page_result;
   
  }
     
$Page_Result = CurlURL("http://shop.renmx.com/json.php");
// curl_setopt($url, CURLOPT_HTTPHEADER, array(//cookie));
$Page_Json_Decode = json_decode($Page_Result);
//print_r($Page_Json_Decode);
var_dump($Page_Json_Decode);

?>
To find your cookie, in chrome at least, right-click on any of the pages, say cards.renmx.com for example.
Click inspect, in the box that pops up click application on the top bar, expand the cookies menu, and click cards.renmx.com
Then you can replace

Code: Select all

//cookie
with:

Code: Select all

"Content-Type : application/x-www-form-urlencoded", "Cookie: __cfduid_VALUE; ULaccount=ULaccount_VALUE; ULkey=ULkey_VALUE; PHPSESSID=PHPSESSID_VALUE;"
keep in mind the something_VALUES are the fields underneath the values bar, corresponding to the proper sections.
Hope this helps!
Last edited by EmberCelica on Tue Nov 15, 2016 4:47 pm, edited 1 time in total.
Image
Image Im the engineer of engineers.
Check out my redstone info thread
User avatar
Pri
Site Admin
Site Admin
Posts: 5433
Joined: Fri Dec 14, 2007 8:59 am

Re: Json to Array with the API's

Sun Nov 13, 2016 9:07 pm

Great post, thank you Ember :)
User avatar
EmberCelica
Engineer
Engineer
CPU: i7 6700k
RAM: 16GB DDR4
Motherboard: [Unknown]
GPU: AMD RX480
Cooling: Fan Cooling
Location: Always behind you
Posts: 188
Joined: Sat Feb 28, 2015 11:20 pm

Re: Json to Array with the API's

Tue Nov 15, 2016 5:49 am

So, i ran into trouble attempting to use the output array.
Attempting to use a foreach, and just return/print specific data from the table using the assosiate array look, it returned:

Code: Select all

Fatal error: Cannot use object of type stdClass as array on line 55
Which, im assuming is due to the following structure, returned using var_dump:

Code: Select all

  ["Ore Booster"]=>
  object(stdClass)#2 (4) {
    ["Type"]=>
    string(4) "Rare"
    ["Obtained"]=>
    string(14) "8th of Jun '16"
    ["Cooldown"]=>
    string(6) "7 Days"
    ["Cooldown Left"]=>
    string(4) "None"
  }
which is nested within the object:

Code: Select all

object(stdClass)#1 (141)
So my question is, how do i process the array, normal handling isnt seeming to work due to the object(stdClass).

Edit: Main code fixed to output associative array by changing the json_decode assoc parameter to true, objectToArray function no longer needed.
Image
Image Im the engineer of engineers.
Check out my redstone info thread

Return to “General Gaming Discussion”

Who is online

Users browsing this forum: No registered users and 8 guests