drgweamoi473182981 2016-03-23 05:52
浏览 67

使用php和mysql记录curl GET请求的结果

I'm trying to understand how to record the result of a curl GET request using php. I'm looking at outputing part or all of the result to mysql.

https://github.com/cloudtrax/docs/blob/master/api/code/php/simple_api_server_test_harness.php

function invoke_curl($method, $endpoint, $headers, $json) {
$api_server = 'https://api.cloudtrax.com';
try {
    // get a curl handle then go to town on it
    $ch = curl_init($api_server . $endpoint);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    $result = curl_exec($ch);
    if ($result == FALSE) {
        if (curl_errno($ch) == 0)
            echo "@@@@ NOTE @@@@: nil HTTP return: This API call appears to be broken" . "
";
        else
            throw new Exception(curl_error($ch), curl_errno($ch));    
    }
    else
      echo "RESULT: 
" . $result . "
";
} 

The $result shows like this:

{
    "clients": {
        "ssid2": 4,
        "ssid1": 10
    },
    "rows": [
        {
            "time": "2016-03-23T02:45:00Z",
            "ssid2": {
                "traffic": {
                    "unclassified": {
//  etc...

How can I associate each part of the result too a variable so I can then input too mysql?

  • 写回答

3条回答 默认 最新

  • duanmei4149 2016-03-23 05:58
    关注

    It looks like this result in json format. You can use json_decode to decode it:

    $resultObject = json_decode($result);
    $clients = $resultObject->clients;
    // ... get other data from result
    
    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建