Page 1 of 1

Json to Array with the API's

Posted: Sun Nov 13, 2016 9:06 pm
by EmberCelica
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!

Re: Json to Array with the API's

Posted: Sun Nov 13, 2016 9:07 pm
by Pri
Great post, thank you Ember :)

Re: Json to Array with the API's

Posted: Tue Nov 15, 2016 5:49 am
by EmberCelica
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.