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

标签 php mysql curl

我试图了解如何使用 php 记录curl GET 请求的结果。我正在考虑将部分或全部结果输出到 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" . "\n";
        else
            throw new Exception(curl_error($ch), curl_errno($ch));    
    }
    else
      echo "RESULT: \n" . $result . "\n";
} 

$结果显示如下:

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

如何将结果的每个部分与变量关联起来,以便我可以输入 mysql?

最佳答案

看起来这个结果是 json 格式的。您可以使用json_decode解码它:

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

关于php - 使用php和mysql记录curl GET请求的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36170755/

相关文章:

php - 如何让基于 curl 的 URL 监控服务轻量级运行?

php - 如何使用 curl 获取 JSON 响应

php - 请解释这个 PHP 函数定义

javascript - 将 randomID 函数从 JS 移植到 PHP

mysql - Python 字节到 geojson 点

php - "curl -v --noproxy localhost"在 php 中的等效项是什么?

php - Glob 模式不匹配任何内容

php - 使用 cPanel 和 Laravel 将电子邮件传输到程序

mysql - 结合两个复杂的查询

mysql - 在mysql中创建一个表作为另一个表