Using PHP to replace JSON encode key

So here is my but of PHP which grabs data and creates my JSON data but im having issues outputting verify-purchase and i think because of the "-" hyphen so want to replace it before i echo is back to my script so its verify_purchase instead as i think this is my issue here but its not working and think im doing it wrong, can anyone help me at all please?

Your see my attempt at the bottom fo the code


// 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 = ''.$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 );

-------------Problems Reply------------


I think this looks easier than what you posed.

$newArray = array();

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

echo json_encode( $newArray );

Please replace above codes to your last two line which is look like below:

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

Let me know if it is not enough per your request.

Hope this is you solution!


Category:php Views:10 Time:2018-09-13
Tags: php json curl

Related post

  • Using PHP to change file encoding generates newlines 2012-02-29

    I use PHP to change file encoding, but it generates a new empty line after each line. $t_comment = file_get_contents('php://stdin'); $t_comment = iconv("ISO-8859-1", "UTF-8//IGNORE",$t_comment); echo $t_comment; Here is myfile.txt: line1 [CR][LF] lin

  • PHP Moving Custom Json Encode/Decode to Abstract Class 2012-02-09

    I am starting to learn PHP and have a decent Java background. I came upon the following coding challenge in the PHP langauge: I want to be able to encode/decode my PHP classes in JSON format. I have created PHP classes that utilize getter/setter meth

  • Using PHP Function in JSON Data 2012-03-27

    I use a shopping cart called Avactis. It returns back to me little "info tags" to use in my front end. They look like this: <?php ProductSmImage();?> <?php ProductName();?> I am trying to use the Agile Carousel script with some of my prod

  • What to use PHP arrays or JSON 2012-02-18

    i am using php arrays in my project like below but i am confused should i use json encode or not what is good for my site here below i get the array returned by the function and loop through it and while using in my UI but i confused how JSON will do

  • Using PHP to replace XHTML tags and add parameters via preg_replace 2011-12-27

    Suppose you have the following code (such portions specified by <art></art> with a unique ID are multiple): <art id="001"> <p class="prim"><h1>word1</h1>Text</p> </art> <art id="002"> <p class=

  • use php to parse json data posted from another php file 2011-01-02

    my web hosting has blocked the outward traffic so i am using a free web hosting to read data and post it to my server but the problem is that my php file receives data in the $_REQUEST variable but is not able to parse it. post.php function postCon($

  • Using PHP instead of JSON in jQuery instant search script 2011-06-04

    I have a Google Instant style search script written in jQuery which pulls results from the JSON BingAPI. How can I make my script pull content from a PHP script rather than the BingAPI? Here is my code: $(document).ready(function(){ $("#search").keyu

  • Using PHP rather than JSON as output in instant search script 2011-06-04

    I have a Google Instant style search script written in jQuery which I want to pull results from a PHP script. I know my script currently needs JSON as the output but I want it to output PHP generated HTML instead. How can I do this? Here is my code:

  • How to use php to replace periods with bullets (li)? 2012-02-01

    I have a string in php with multiple periods. I am trying to figure out to replace each of the periods in the string into a bullet (<li>) on a new line. Does anyone have advice on how to do this? $desc= "Petite Jeans. Petite Fit Guide,Tall Fit

  • Using PHP to store json via Instagram API 2012-04-14

    I feel like I'm overthinking this. What I want to do is pull the most recent photos from the instagram api and save the resulting json information as a cache file. I'll then use jQuery to read from that file -- I've got that part figured out. What I'

  • Invalid PHP JSON encoding 2009-11-24

    I'm working on a project in PHP (5.3.1) where I need to send a JSON string to a webservice (in python), but the result I get from json_encode does not pass as a valid JSON (i'm using JSLint to check validity). I should add that the structure I'm tryi

  • How can I create two related tables in MySQL and return a result set as JSON using PHP? 2011-07-08

    I need to create two related tables in MySQL and return a result set as JSON using PHP. I have figured out how to get my data from MySQL using PHP and how to encode it to JSON, but I'm not sure how to use Javascript to display the JSON results on my

  • Replacing javascript functions using PHP 2009-07-10

    I want to use PHP to replace javascript functions in HTML documents. For example: original: function my_function(hey) { do stuff } new: function new_function(hi) { do different stuff } I was thinking of using regular expressions with the ereg_replace

  • idea to add javascript array to database using PHP 2010-08-19

    i need to push data in javascript array into database using PHP first i have multidimentional array in js. Brady [0] [0] = "1"; Brady [0] [1] = "Carol"; Brady [1] [0] = "2"; Brady [1] [1] = "Jan"; Brady [2] [0] = "3"; Brady [2] [1] = "Mike"; and i ha

  • JSON Encoding - Get MySQL data to an iPhone 2011-06-02

    Im new to JSON and have whats probably a basic question. I have some MySQL rows I want grab with Php, and then JSON encode for a response to an iphone request. so the questions are: 1) Whats the format for the array to encode this info? 2) Where the

  • JSON Encode problems 2012-01-12

    Im trying to create this with the php encode function: { "foo": [ { "bar": "111" } ] } But all i can manage with some php arrays and json encoding is this: { "foo": [ "{ \"bar\":184530" }" ] } Obviously i don't want the object as a string but as an o

  • Android, Connecting to MySQL using PHP: Null Pointer exception 2012-03-03

    Im a newbie to android, I am learning to connect to a server through android client using Php, MySql and JSON. For testing purpose im running on localhost. So for here's what I've done. Database demo.php public class Database_demo extends ListActivit

  • Php search replace interesting task 2011-04-19

    I have an interesting puzzle to solve. We have to convert the following variable using PHP find replace command from: $string = ' h= 1.00 h= 2.00 h= 3.50 W= 1.50 w= 3.00 w= 4.50 st=5000 st=6000 st=7000 '; to: $string = ' A=1.00, B=2.00, C=3.50 A=1.50

  • How can i use php's preg_replace with a simple string and wildcards? 2011-08-13

    If i have the string [link="*"] where * is a wildcard how could i then use php to replace the string with <a href="*"> where * is the same value as before? Is preg_replace the best way to do this? Thanks, any help appreciated! --------------Sol

  • reading json encoded php array using ajax request 2011-03-17

    I have a php array with key, value pairs. I have json encoded it. And using ajax.Request i called the php file in which that array is there. Now i have to access both key and value pairs. Could anyone let me know how to do that? --------------Solutio

  • Sort a JSON-Encoded List of X and Y Coordinates using PHP 2011-12-12

    Using PHP, I must operate on a JSON-encoded stream that was initialized in the following manner: set = new Array(); set[n] = {"index": new Array()}; set[n].index[i] = {"x": xLoc, "y": yLoc, "id": "id#"}; Which ultimately exports to a JSON-encoded str

  • How to replace Microsoft-encoded quotes in PHP 2009-08-11

    I need to replace Microsoft Word version of single and double quotations marks (“ ” ‘ ’) with regular quotes (' and ") due to an encoding issue in my application. I do not need them to be HTML entities and I cannot change my database schema. I have t

  • How to generate json using php? 2009-09-16

    I've never used JSON before and I'm trying to utilize the following javascript: It needs a JSON output in the following format: [{oV: 'myfirstvalue', oT: 'myfirsttext'}, {oV: 'm

  • How to use regular expression in json(PHP)? 2010-02-09

    Now i have a json code like this: {"1": { "text":"e1", "children": { "1": { "text":"e1_site1", "children": {"1":"e1_site1_salarie1_nom"} }, "3": { "text":"e1_site2", "children": { "3":"e1_site2_sa1", "4":"e1_site2_sa2" } } } }, "2": { "text":"e2", "c

Copyright (C), All Rights Reserved.

processed in 0.156 (s). 11 q(s)