如何将api结果添加到mysql

时间:2015-03-18 03:03:27

标签: php mysql arrays json api

我正在研究一个项目,并试图找到一种方法将我的api结果添加到mysql。

以下是the page您可以找到结果。

以下是该页面的代码:

    <?php
///PLOT PROJECT USER REQUEST

//HELPER FUNCTION TO PRINT TO CONSOLE
function debug_to_console( $data ) {

    if ( is_array( $data ) )
        $output = "<script>console.log( 'Debug Objects: " . implode( ',', $data) . "' );</script>";
    else
        $output = "<script>console.log( 'Debug Objects: " . $data . "' );</script>";

    echo $output;
}

//PLOT PROJECT REQUEST 
$appId = '9fed0c75ca624e86a411b48ab27b3d5a';
$private_token = 'VGjPehPNBa5henSa';
$qry_str = "/api/v1/account/";

$ch = curl_init();
$headers = array(
'Content-Type:application/json',
'Authorization: Basic '. base64_encode($appId.":".$private_token) // <---
);

$geofenceId = '736cb24a1dae442e943f2edcf353ccc7'; 

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, 'https://admin.plotprojects.com/api/v1/notification/?geofenceId=' . $geofenceId); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, '3');
curl_setopt($ch, CURLOPT_ENCODING, 'gzip');

$content = trim(curl_exec($ch));
curl_close($ch);
print $content;

?>

1 个答案:

答案 0 :(得分:0)

您可以使用json_decode()解码您的JSON字符串,并获取进行查询所需的值。阅读更多内容:

  • http://php.net/manual/en/function.json-decode.php

    <?php
    
    //The response from http://www.jobsinsac.com/api/api_notification.php
    
    $json = '{ "success": true, "result": { "data": [{ "placeId": "736cb24a1dae442e943f2edcf353ccc7", "cooldownDays": 0, "triggerTimes": "inherit", "state": "published", "cooldownSeconds": 1, "data": "http://www.illuminatimc.com", "enabled": true, "geofenceId": "736cb24a1dae442e943f2edcf353ccc7", "id": "0cad6b54d225459e85cd8c27567f8b0b", "message": "Get a cold beer, for $2.00, shots for $4.00, come inside, up stairs.", "created": "2015-03-17T18:54:41Z", "timespans": [], "handlerType": "landingPage", "trigger": "enter" }], "total": 1 } }';
    
    $data = json_decode($json, true);
    
    print_r($data);
    
    ?>
    

    输出:

    Array
    (
        [success] => 1
        [result] => Array
            (
                [data] => Array
                    (
                        [0] => Array
                            (
                                [placeId] => 736cb24a1dae442e943f2edcf353ccc7
                                [cooldownDays] => 0
                                [triggerTimes] => inherit
                                [state] => published
                                [cooldownSeconds] => 1
                                [data] => http://www.illuminatimc.com
                                [enabled] => 1
                                [geofenceId] => 736cb24a1dae442e943f2edcf353ccc7
                                [id] => 0cad6b54d225459e85cd8c27567f8b0b
                                [message] => Get a cold beer, for $2.00, shots for $4.00, come inside, up stairs.
                                [created] => 2015-03-17T18:54:41Z
                                [timespans] => Array
                                    (
                                    )
    
                                [handlerType] => landingPage
                                [trigger] => enter
                            )
    
                    )
    
                [total] => 1
            )
    
    )