0

Good Day, How do I get data from this array ?

Array
(
   [input] => Array
    (
        [account] => XXXXX
        [mass] => 0.6
        [quantity] => 5
        [length] => 15
        [width] => 15
        [height] => 15
        [to_town] => Somerset West
        [from_town] => Honeydew
        [delivery_type] => D/D
        [cod] => 
        [response_type] => 
        [town_strict] => 
    )

[to_town_valid] => Y
[from_town_valid] => Y
[result] => Success
[amount] => Array
    (
        [rate] => UQ
        [chargeable_mass] => 3.38
        [excl_vat] => 250.43
        [incl_vat] => 287.99
        [cod_charge] => 0
        [fuel_surcharge] => 54.17
        [volumetric] => 5000.00
        [delivery_surcharge] => 0.00
        [packaging_surcharge] => 0.00
        [vat] => 46.00
        [total] => 350.29
    )

)

1


My code as follows but not working :(

$myurl = "http://timefreight.co.za/collection/api/rates_api_block.php?account={$key}&mass={$mas}&quantity={$qa}&length={$len}&width={$wid}&height={$hei}&to_town={$to}&from_town={$fro}&delivery_type=D/D";

$results = file_get_contents($myurl);

return $results['amount']['total'];

I tried $result = json_decode($results, true); but it returns nothing :(

Any help will be appreciated !

Siva
  • 1,481
  • 1
  • 18
  • 29
Andrew
  • 1
  • you decode the json string first, after that you'd be able to get those data that you want, here's the precedence `$results = json_decode(file_get_contents($myurl), true)`, use decode after you use get contents, then access it like any normal array – Kevin Oct 03 '18 at 08:18
  • Change the `rates_api_block.php` to return a JSON String. Currently I dont think it is, its returning a `print_r()` at a guess – RiggsFolly Oct 03 '18 at 08:18
  • Show us the code for the `rates_api_block.php` script please. **Edit it into your questions** – RiggsFolly Oct 03 '18 at 08:19
  • `rates_api_block.php` is also returning a spurious `1` as well. So lets help you fix that script – RiggsFolly Oct 03 '18 at 08:21
  • Thank you all, I tried all of the above and still no luck :( I unfortunately dont have access to the rates_api_block.php script. – Andrew Oct 03 '18 at 08:29
  • if you print the $results , what data is inside ? is it empty or not ? – Yassine CHABLI Oct 03 '18 at 08:38
  • Hi Mohammed, I get the above Array – Andrew Oct 03 '18 at 08:48
  • Is there another parameter you can add to the querystring to make it return a JSON String. Have a look at the documentation. This looks like the way you might test the API rather than how you would be expected to use it – RiggsFolly Oct 03 '18 at 08:51
  • Hi RiggsFolly, There are no other parameters – Andrew Oct 03 '18 at 09:26
  • Possible duplicate of [How create an array from the output of an array printed with print\_r?](https://stackoverflow.com/questions/7025909/how-create-an-array-from-the-output-of-an-array-printed-with-print-r) – splash58 Oct 03 '18 at 09:47
  • Not a duplicate, tried that code and didn't work for me either – Andrew Oct 03 '18 at 12:31

0 Answers0