donglei1973 2012-07-05 23:04
浏览 16
已采纳

使用cURL和PHP操作对象

Included below is a sample of the data found in the file 1.php:

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

This is my code so far, and I'm not getting very far so any help appreciated.

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

?>

Basically, I would like to display "ID":"1" as a name, such as "Meat" "ID":"2" "Cheese" etc. Then display a queue wait time which is the value of "wait" next to the name. The shop is only open from 10am-5pm so outside of these hours I just want it to display the name and "Closed" next to it insead of the wait time.

Lastly if the wait time is greater than 10 mins, then I would like to deduct 5 mins from that value.

I am unable to get the code above to work as expected and would really appreciate a hand.

Many Thanks

  • 写回答

1条回答 默认 最新

  • douyanpeng0748 2012-07-06 00:10
    关注

    I've taken a few liberties in trying to get what you want, so let me know how close it is:

    // 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 />';
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大