使用PHP替换JSON编码密钥

时间:2015-06-04 11:00:57

标签: php json curl

这里有一些PHP可以抓取数据并创建我的JSON数据,但是我遇到输出验证购买的问题。我认为因为“ - ”连字符所以想要在我回到我的脚本之前替换它所以它是verify_purchase而不是。我认为这是我的问题,但它不起作用,并认为我做错了。

你看到我在代码底部的尝试。

// 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['data'] = str_replace('verify-purchase','verify_purchase', $result['data']);
echo json_encode( $result );

1 个答案:

答案 0 :(得分:1)

我认为这看起来比你提出的要容易。

$newArray = array();

$findKey = 'verify-purchase';//Find what is exist now
$replaceKey = 'verify_purchase';//replace what you want
foreach($arr[""data""] as $key => $value){
    if(isset($arr[""data""][$findKey])){
       $newArray[$replaceKey] = $value;
    }
}

echo json_encode( $newArray );

请将以上代码替换为最后两行,如下所示:

$result['data'] = str_replace('verify-purchase','verify_purchase', $result['data']);
echo json_encode( $result );

根据您的要求,请告诉我。