0

I've been searching for a while but I couldn't find a correct way to solve my problem.

Above all I try to sort out my problem following these two answers:

How to use cURL to get jSON data and decode the data?

and

How to parse out variables from a JSON var_dump in a Wordpress Plugin

but they didn't solve my problem.

So, this is my issue: I got this api:

http://data.xxxxxx.xxx/ws.php?username=xxxxxx&format=1&output=json&compress=0&latmin=38.7&latmax=39&lonmin=9.3&lonmax=9.4

and I can use only CURL library or fsockopen to retrieve the data.

I'm trying to use the CURL library on this way:

<?php
$url = 'http://data.aishub.net/ws.php?username=xxxxxxxxxx&format=1&output=json&compress=0&latmin=38.7&latmax=39&lonmin=9.3&lonmax=9.4';
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$url);
$result=curl_exec($ch);
curl_close($ch);
var_dump(json_decode($result, true));
$array[0][1][0]["MMSI"];
echo $array;
?>    

and I get this answer:

    array(2) { [0]=> array(8) { ["ERROR"]=> bool(false) ["USERNAME"]=> string(16) "xxxxxxxxxx" 
["FORMAT"]=> string(5) "HUMAN" ["LATITUDE_MIN"]=> float(38.5) ["LATITUDE_MAX"]=> int(39) 
["LONGITUDE_MIN"]=> float(9.3) ["LONGITUDE_MAX"]=> float(9.5) ["RECORDS"]=> int(2) } [1]=> array(2) 
{ [0]=> array(19) { ["MMSI"]=> int(636012570) ["TIME"]=> string(23) "2015-05-24 15:58:13 GMT" 
["LONGITUDE"]=> float(9.32533) ["LATITUDE"]=> float(38.79291) ["COG"]=> float(145.6) ["SOG"]=> 
float(13.7) ["HEADING"]=> int(511) ["NAVSTAT"]=> int(0) ["IMO"]=> int(9144794) ["NAME"]=> 
string(9) "CE NIRIIS" ["CALLSIGN"]=> string(5) "A8GG6" ["TYPE"]=> int(81) ["A"]=> int(205) ["B"]
=> int(38) ["C"]=> int(24) ["D"]=> int(18) ["DRAUGHT"]=> int(8) ["DEST"]=> string(18) "LA SKHIRRA TUNISIA" 
["ETA"]=> string(11) "05-26 02:00" } [1]=> array(19) { ["MMSI"]=> int(247325500) ["TIME"]=>
 string(23) "2015-05-24 15:56:22 GMT" ["LONGITUDE"]=> float(9.4617) ["LATITUDE"]=> float(38.89681)
 ["COG"]=> float(123.2) ["SOG"]=> float(10.5) ["HEADING"]=> int(120) ["NAVSTAT"]=> int(0) 
["IMO"]=> int(9346902) ["NAME"]=> string(9) "SYN TABIT" ["CALLSIGN"]=> string(4) "IBEK" 
["TYPE"]=> int(1) ["A"]=> int(72) ["B"]=> int(23) ["C"]=> int(11) ["D"]=> int(4) ["DRAUGHT"]=> 
float(6.4) ["DEST"]=> string(12) "THESSALONICO" ["ETA"]=> string(11) "05-28 17:00" } } }

In this format:

array(2){[0]{part a}[1]{[0]{PART A}[1]{PART B}}}

I don't need retrieve the data on the "part a", but I need to work on PART A and PART B.

My intention is to search a value (ship name) in the PART A and retrieve its correspondent values as MMSI, latitude, longitude inside the same row.

The PART A & PART B in the reality will be about 10.000 parts, and not only 2. In my example I used var_dump to check all the results and the echo to compare if I can get the right result because my data are always change.

Can you please help me on this please?

Community
  • 1
  • 1
Aldo
  • 13
  • 8
  • If you're not retrieving the information you want, my *guess* is that you're using the wrong query parameters in your `$url`. – timgavin May 24 '15 at 16:27
  • @timgavin: If the query parameters wouldn't be correct, I won't be able to see the results I get. Isn't it? My problem is I can't get the single value inside the array – Aldo May 24 '15 at 16:34
  • Why is this a cURL problem? It seems like you just need help getting to the array part. – Jared Farrish May 24 '15 at 16:36
  • @Aldo Well, you said "I don't need retrieve the data on the "part A", but I need to work on PART A". Does this mean you're not retrieving part A? If so, you may be missing a query parameter, or using the incorrect one. – timgavin May 24 '15 at 16:48
  • @timgavin: I edited the answer: 'array(2){[0]{part a}[1]{[0]{PART A}[1]{PART B}}}'. I don't need do anything in 'part a' but i need to do something in 'PART A' and 'PART B'. Probably the problem it is to set up the array as jared and Raphael as said, but I'm trying in a lot of way but I can sort it out the right way. – Aldo May 24 '15 at 16:57
  • @Jared Farrish: I didn't say it was a CURL problem, I just say I have problem to sort out the value I wanted and ask if someone could help me. – Aldo May 24 '15 at 17:05
  • Fix the title to introduce the problem. The current tile seems to suggest your problem is a cURL/web service problem, when you don't know how to access an array to get what you need. – Jared Farrish May 24 '15 at 17:27
  • @Jared Farrish: I changed the title – Aldo May 24 '15 at 17:44

2 Answers2

0

You are using json_decode() to display the data inside var_dump but do not save the decoded array to a variable you can use later on.

$result=curl_exec($ch);
curl_close($ch);
$shipsArray = json_decode($result, true);
var_dump($shipsArray);
/* do something with your data */
echo $shipsArray[0][1][0]["MMSI"];
Hafenkranich
  • 1,696
  • 18
  • 32
  • Thanks for your answer. This is the point I can't sort out. I did try to save the array into a variable but this is the point I can't do. Can you please help me on this? – Aldo May 24 '15 at 17:23
  • @Aldo I think it's already solved by Brian in the meantime, right? – Hafenkranich May 25 '15 at 20:26
  • Saving the data into an array is exactly what he does by ` $data = json_decode($json, true); // data array` (It's the same in my code in line 3) – Hafenkranich May 25 '15 at 20:27
  • 1
    I didn't understand it before sorry. Now I'm trying to search a list of results from a `row` in the mySQL and look in the `$value['NAME`] in the web service and reuse the other values given me from the web service. This is my next step I'm facing to. – Aldo May 26 '15 at 18:31
0

Complete update to include $response error check and search comparison in the form of a function.

<?php
Function SO_Aldo($srch) {

    $result = "";

    $url = 'http://data.aishub.net/ws.php?username=xxxxxxxxxx&format=1&output=json&compress=0&latmin=38.7&latmax=39&lonmin=9.3&lonmax=9.4';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL,$url);
    $json = curl_exec($ch);
    curl_close($ch);

    $data = json_decode($json, true); // data array
    $response = $data[0]; // the data[0] response ... Error, username, format, etc
    unset($data[0]); // remove data response from array leaving only results
    #var_dump($data); // debugging
    #var_dump($response); // debugging

    // Check for Response ERROR
    IF ($response['ERROR']) { 

        $result = "Curl Response Error";

    }ELSE{

        foreach($data as $index => $records) {
            foreach ($records as $key => $value) {

                // compare srch against name
                IF ($value['NAME'] == $srch) {
                    $result = "<p>Name: ".$value['NAME']."<br>MMSI: ".$value['MMSI']."<br>Longitude: ".$value['LONGITUDE']."<br>Latitude: ".$value['LATITUDE']."</p>";
                }

            }
        }
    }

    IF (empty($result)) { $result = "No Results"; }
    return $result;

}

// usage
$srch = "SYN TABIT"; // $_POST Value to search for
$result = SO_Aldo($srch);
echo($result);

?>
Brian
  • 1,035
  • 7
  • 14
  • If you want to do a search within the $result I need to know exactly what $value['KEY'] you wish to compare on. (MMSI, NAME etc. etc.) – Brian May 24 '15 at 20:08
  • Thanks a lot Brian, your answer is very useful. My search will be, searching from the NAME and get the other results (LATITUDE, LONGITUDE, MMSI etc.) and reuse them as number. – Aldo May 24 '15 at 20:11
  • Updated my answer. I turned it into a function for easier usage. Added a response error check and included the search functionality. Glad I could help. – Brian May 24 '15 at 20:37
  • Thank you very much, your code which is perfectly working. I will be trying now to make my website who is basically a database make the request to the web service above and retrieve the data I desire. Thanks again for your useful and perfect help. – Aldo May 24 '15 at 22:09