Using PHP to replace JSON encode key -
so here of php grabs data , creates json data im having issues outputting verify-purchase , think because of "-" hyphen want replace before echo script verify_purchase instead think issue here not working , think im doing wrong, can me @ please?
your see attempt @ bottom fo code
php
// make curl request $ch = curl_init(); curl_setopt($ch, curlopt_useragent, "mozilla/5.0 (iphone simulator; u; cpu iphone os 4_3_2 mac od x; en-us) applewebkit/535.17.9 (khtml, 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 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 activate license " . $item_name . ". envato states purchase code owned user."; } } else if ( $json_data['error'] ) { $result['code'] = 112; $result['msg'] = 'failed activate license. envato api request returned: ' . $json_data['error']; } else { $result['code'] = 121; $result['msg'] = 'failed activate license. envato states purchase code ' . $envato_purchase_code . ' not valid.'; } } } $result['data'] = str_replace('verify-purchase','verify_purchase', $result['data']); echo json_encode( $result );
james,
i think looks easier posed.
$newarray = array(); $findkey = 'verify-purchase';//find exist $replacekey = 'verify_purchase';//replace want foreach($arr[""data""] $key => $value){ if(isset($arr[""data""][$findkey])){ $newarray[$replacekey] = $value; } } echo json_encode( $newarray );
please replace above codes last 2 line below:
$result['data'] = str_replace('verify-purchase','verify_purchase', $result['data']); echo json_encode( $result );
let me know if not enough per request.
hope solution!
thanks
Comments
Post a Comment