PHP使JSON数据无法正常工作

时间:2015-06-03 18:23:48

标签: php json

有谁知道如何从JSON数据中获取数据

{  
   "data":{  
      "verify-purchase":{  
         "item_name":"Simplified PHP Invoice \/ Billing System",
         "item_id":"11438884",
         "created_at":"Sun May 31 07:49:31 +1000 2015",
         "buyer":"aurysilva",
         "licence":"Regular License"
      }
   },
   "code":200,
   "msg":"SUCCESS. Simplified PHP Invoice \/ Billing System License Activated. Purchase date: Sun May 31 07:49:31 +1000 2015"
}

如果我这样做:

<?php

    $LE = new License_Enforcer( 'http://www.rebootdigital.co.uk/verify.php');
    $username = LICENSE_USERNAME;
    $purchase_code = LICENSE_KEY;
    $verified = $LE->verify($username, $purchase_code);

    // save the result
    $license_verified = $verified;

    //$class = $LE->return_code >= 200 ? 'update' : 'error';
    //echo sprintf("<div id='message' class='%s'><p>%s</p></div>", $class, $LE->msg);

    echo $LE->msg;
    echo $LE->data->item_name;

?>

$LE->msg返回msg数据,但我不知道如何获取实际数据。

JSON代码:

// Make curl request
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (iPhone Simulator; U; CPU iPhone OS 4_3_2 like Mac OD X; en-us) AppleWebKit/535.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8H7 Safari/6533.18.5");
    $url = 'http://marketplace.envato.com/api/edge/'.$envato_author_username.'/'.$envato_author_apikey.'/verify-purchase:'.$envato_purchase_code.'.json';
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $ch_data = curl_exec($ch);
    curl_close($ch);

    if ( empty( $ch_data ) ) { # no data returned - Timeout or similar
        $result['code'] = 111;
        $result['msg'] = 'FAILED to Activate License. Envato API request returned: ' . curl_error( $ch ) . " Please try again later.";
    }
    else {
        $json_data = json_decode( $ch_data, true );
        $result['data'] = $json_data;
        if ( isset( $json_data['verify-purchase'] ) && count( $json_data['verify-purchase']) > 0 ) {
            $license_type = $json_data['verify-purchase']['licence'];
            $item_name = $json_data['verify-purchase']['item_name'];
            $item_id = $json_data['verify-purchase']['item_id'];
            $buyer = $json_data['verify-purchase']['buyer'];
            $created_at = $json_data['verify-purchase']['created_at'];
            if ( strcasecmp( $envato_username, $buyer ) === 0 ) {
                $result['code'] = 200;
                $result['msg'] = "SUCCESS. " . $item_name . " License Activated. Purchase date: " . $created_at;
            }
            else {
                $result['code'] = 122;
                $result['msg'] = "FAILED to Activate License for " . $item_name . ". Envato states this purchase code is owned by another user.";
            }
        } else if ( $json_data['error'] ) {
            $result['code'] = 112;
            $result['msg'] = 'FAILED to Activate License. Envato API request returned: ' . $json_data['error'];
        } else {
            $result['code'] = 121;
            $result['msg'] = 'FAILED to Activate License. Envato states purchase code ' . $envato_purchase_code . ' is not valid.';
        }

        $result = str_replace('verify-purchase','verify_purchase',$data);
    }
}

echo json_encode( $result );
echo "\n";

// update log file
$logfile = dirname(__FILE__) . '/' . basename(__FILE__, '.php') . '.log';

$fh = @fopen( $logfile ,"a" );
if ($fh) {
    fwrite( $fh, date( 'Y-m-d H:i:s',time() ) . ' ');
    fwrite( $fh, $envato_username . ' ' );
    fwrite( $fh, $envato_purchase_code . ' ' );
    fwrite( $fh, $website  . ' ');
    fwrite( $fh, isset( $item_name ) ? '"' . $item_name . '"' : '-' );
    fwrite( $fh, ' ==> ' );
    fwrite( $fh, $result['code'] . ' ' );
    fwrite( $fh, '"' . $result['msg'] . '" ');
    fwrite( $fh, "\n" );
    fclose($fh);
}
?>

1 个答案:

答案 0 :(得分:0)

anant的anwer缺少名称'verify-purchase'周围的报价。请使用引号再次尝试:

echo $LE->data->{'verify-purchase'}->item_name;