【问题标题】:Array issue retrieving data from web service using curl library/PHP使用 curl 库/PHP 从 Web 服务检索数据的数组问题
【发布时间】:2015-05-24 16:24:47
【问题描述】:

我已经搜索了一段时间,但找不到解决问题的正确方法。

首先,我尝试按照以下两个答案来解决我的问题:

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

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

但他们没有解决我的问题。

所以,这是我的问题: 我得到了这个 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

我只能使用 CURL 库或 fsockopen 来检索数据。

我正在尝试以这种方式使用 CURL 库:

<?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;
?>    

我得到了这个答案:

    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" } } }

采用这种格式:

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

我不需要检索“part a”的数据,但我需要处理 PART A 和 PART B。

我的目的是在 PART A 中搜索一个值(船名)并在同一行中检索其对应的值,如 MMSI、纬度、经度。

现实中的PART A & PART B 大约有 10.000 个零件,而不仅仅是 2 个。 在我的示例中,我使用var_dump 来检查所有结果,并使用echo 来比较我是否可以获得正确的结果,因为我的数据总是在变化。

你能帮我解决这个问题吗?

【问题讨论】:

  • 如果您没有检索到您想要的信息,我的猜测是您在$url 中使用了错误的查询参数。
  • @timgavin:如果查询参数不正确,我将无法看到我得到的结果。不是吗?我的问题是我无法获得数组中的单个值
  • 为什么这是一个 cURL 问题?看来您只需要帮助才能进入数组部分。
  • @Aldo 好吧,你说“我不需要检索“A 部分”的数据,但我需要处理 A 部分”。这是否意味着您没有检索 A 部分?如果是这样,您可能缺少查询参数,或者使用了不正确的参数。
  • @timgavin:我编辑了答案:'array(2){[0]{part a}[1]{[0]{PART A}[1]{PART B}}}' .我不需要在“A 部分”中做任何事情,但我需要在“A 部分”和“B 部分”中做一些事情。可能问题在于将数组设置为 jared 和 Raphael 所说的那样,但我正在尝试很多方法,但我可以以正确的方式对其进行排序。

标签: php web-services curl


【解决方案1】:

完整更新,以函数的形式包含 $response 错误检查和搜索比较。

<?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);

?>

【讨论】:

  • 如果您想在 $result 中进行搜索,我需要确切知道您希望比较的 $value['KEY'] 是什么。 (MMSI、NAME 等)
  • 非常感谢布赖恩,您的回答非常有用。我的搜索将是,从 NAME 搜索并获得其他结果(纬度、经度、MMSI 等)并将它们作为数字重复使用。
  • 更新了我的答案。我把它变成了一个更容易使用的函数。添加了响应错误检查并包括搜索功能。很高兴我能帮上忙。
  • 非常感谢,您的代码运行良好。我现在将尝试让我的网站(基本上是一个数据库)向上面的 Web 服务发出请求并检索我想要的数据。再次感谢您的有用和完美的帮助。
【解决方案2】:

您正在使用 json_decode() 来显示 var_dump 中的数据,但不要将解码后的数组保存到以后可以使用的变量中。

$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"];

【讨论】:

  • 感谢您的回答。这是我无法解决的问题。我确实尝试将数组保存到变量中,但这是我做不到的。你能帮我解决这个问题吗?
  • @Aldo 我想这期间 Brian 已经解决了,对吧?
  • 将数据保存到数组中正是他所做的` $data = json_decode($json, true); // 数据数组`(在我的第 3 行代码中是一样的)
  • 抱歉之前没看懂。现在我正在尝试在 mySQL 中搜索来自 row 的结果列表,并在 Web 服务中查看 $value['NAME] 并重用从 Web 服务给我的其他值。这是我面临的下一步。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多