使用 cURL 和 PHP 操作对象


Manipulate object using cURL and PHP

下面

包括在文件1.php中找到的数据示例:

: [
    {
        "ID": "1",
        "active": "1",
        "wait": "30"
    },
    {
        "ID": "6",
        "active": "1",
        "wait": "10"
    },
    {
        "ID": "8",
        "active": "1",
        "wait": "65"
    }
]
}

到目前为止,这是我的代码,我还没有走得太远,所以任何帮助都值得赞赏。

<?php
$url = "1.php";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec ($ch);
curl_close ($ch);  
if((date("H") < 9) || (date("H") > 24)) {
    echo "Closed";
} else {
    echo ($Queue_Time: <= 10) ? $data : $Queue_Time-5;
};
$result = json_decode ($json);
$arr = array(1, 2);
?>
基本上,我想显示"ID":"

1"作为名称,例如"肉"ID":"2"奶酪"等。然后显示队列等待时间,即名称旁边的"等待"值。这家商店只在上午 10 点至下午 5 点开放,所以在这些时间之外,我只希望它在等待时间旁边显示名称和"关闭"。

最后,如果等待时间大于 10 分钟,那么我想从该值中扣除 5 分钟。

我无法让上面的代码按预期工作,非常感谢。

非常感谢

我在尝试获得您想要的东西时采取了一些自由,所以让我知道它有多接近:

// Assuming we have a correctly formatted JSON array from CURL
$data = '[
    {
        "ID": "1",
        "active": "1",
        "wait": "30"
    },
    {
        "ID": "6",
        "active": "1",
        "wait": "10"
    },
    {
        "ID": "8",
        "active": "1",
        "wait": "65"
    }
]
';
// This array maps IDs to Names
$department[1] = 'Meat';
$department[2] = 'Cheese';
$department[6] = 'Fish';
$department[8] = 'Vegetables';
// Decode the JSON data
$data = json_decode($data);
// Loop through each area
foreach ($data as $counter) {
    // Check if closed
    if((date("H") < 10) || (date("H") > 17)) {
        $queue = "Closed";
    } else {
        $queue = ($counter->wait <= 10) ? $counter->wait : $counter->wait-5;
    };
    // Output name and wait time
    echo $department[$counter->ID].' '.$queue.'<br />';
}