简体   繁体   English

PHP获取JSON数据不起作用

[英]PHP getting JSON data not working

Does anyone know how I can get the data from the JSON data 有谁知道我如何从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"
}

If I do: 如果我做:

<?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 returns the msg data, but I am not sure how to get the actual data. $LE->msg返回msg数据,但是我不确定如何获取实际数据。

JSON code: 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);
}
?>

anant's anwer is missing quotes around the property name 'verify-purchase'. anant的答案缺少属性名“ verify-purchase”周围的引号。 Try it again with quotes: 再次尝试使用引号:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM