<?php
function get_stuff()
{
($_GET['http://****RemovedForSecurityPurposes****.repairshopr.com/api/v1/tickets?api_key=****RemovedForSecurityPurposes****']);
echo $_GET;
}
get_stuff();
?> /* Yes, Pointless closing tag, I'm cool like that. */
For some reason when I run this code the output I'm getting is "Array" and I can't figure out why? I know it is an array that I'm getting from the URL but I thought it would just print in whatever format its in? Am I missing something?
Thanks In advance!
You cannot pass website URL to $_GET.
You should use file_get_contents()
$content = file_get_contents('http://YOUR_URL');
If you have valid json then you can convert it to an array.
$data = json_decode($content, true);
then print it,
echo '<pre>'; print_r($data);
The Message of OP when I run this code the output I'm getting is
"Array".
You need to use file_get_contents to read a file from a remote server. If your file response an array then you have to use print_r or var_export or var_dump. Or if your file response is a string(json) then you need to store it in a variable and apply a decode method.
function get_stuff(){
$response = file_get_contents('http://****RemovedForSecurityPurposes****.repairshopr.com/api/v1/tickets?api_key=****RemovedForSecurityPurposes****');
print_r($response); // if array
$arr = json_decode($response); // decode of json
print_r($arr);
}
get_stuff();
I think you will understand what i mean. Let me know if you are useful or need some help.
Related
I run the following PHP after I submit a form and get the output shown below:
$data = json_encode($_POST);
// output
{"First_Name":"Fred"}
How do I use PHP to just display the value 'Fred'?
I tried echo $data['First_Name']; but this is blank.
You no need to encode your incoming $_POST data.
Just say:
echo $_POST['First_Name'];
If you get a json data, decode it into an array:
$data = '{"First_Name":"Fred"}';
$decoded = json_decode($data, true);
echo $decoded['First_name'];
First of all, don't know why you use json_encode on PHP Array, and try to access it like it's an array - because after json_encode it's a string.
You have to use json_decode($data, true) and then you can access it like $data['First_Name'] or try to access it directly without json_encode() by $_POST['First_Name']
The json_decode() function is used to decode or convert a JSON object to a PHP object.And try to put the object to decode in another variable to avoid errors
<?php
$obj = '{"First_Name":"Fred"}';
$data = json_decode($obj);
echo ($data->First_Name);
?>
anyone can help to decode this?
var_dump
sring(56441) "{"success":{"message":"PDF \u0444\u0430\u0439\u043b\u0430 \u0435 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0430\u043d \u0443\u0441\u043f\u0435\u0448\u043d\u043e!","name":"invoice-0000000001.pdf","data":"JVBERi0xLjQKMSAwIG9iago8PAovVGl0bGUgKP7\/BCQEMAQ6BEIEQwRABDAAICEWADAAMAAwADAAMAAwADAAMA","type":"application\/pdf"}}"
I need to get "data" from this code.
<?php
$data='{"success":{"message":"PDF \u0444\u0430\u0439\u043b\u0430 \u0435 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0430\u043d \u0443\u0441\u043f\u0435\u0448\u043d\u043e!","name":"invoice-0000000001.pdf","data":"JVBERi0xLjQKMSAwIG9iago8PAovVGl0bGUgKP7\/BCQEMAQ6BEIEQwRABDAAICEWADAAMAAwADAAMAAwADAAMA","type":"application\/pdf"}}';
$data=json_decode($data,true);
echo '<pre>';
print_r($data['success']['data']);
It's a simple json that you need to make it an array and access it as a normal array. The output is the data field you asked:
JVBERi0xLjQKMSAwIG9iago8PAovVGl0bGUgKP7/BCQEMAQ6BEIEQwRABDAAICEWADAAMAAwADAAMAAwADAAMA
Well, you only need to json_decode() that string and you have your result:
<?php
$input = '{"success":{"message":"PDF \u0444\u0430\u0439\u043b\u0430 \u0435 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0430\u043d \u0443\u0441\u043f\u0435\u0448\u043d\u043e!","name":"invoice-0000000001.pdf","data":"JVBERi0xLjQKMSAwIG9iago8PAovVGl0bGUgKP7\/BCQEMAQ6BEIEQwRABDAAICEWADAAMAAwADAAMAAwADAAMA","type":"application\/pdf"}}';
$output = json_decode($input);
print_r($output->success->data);
JVBERi0xLjQKMSAwIG9iago8PAovVGl0bGUgKP7/BCQEMAQ6BEIEQwRABDAAICEWADAAMAAwADAAMAAwADAAMA
i cant for the life of me figure out why i cant get this JSON to work
this is how it comes back from api
"{"errorCode":5,"errorDescription":"Unknown account"}"
i have tried all of the following to get "Unknown account"
$error = $result['errorCode":5,"errorDescription'];
$error = $result['errorCode'];
$error = $result['errorDescription'];
i would be ok even just trying for error code 5, so i can do a check to further the script if account is made or not.
any ideas what i am missing?
try this:
$json = '{"errorCode":5,"errorDescription":"Unknown account"}';
$arr = json_decode($json, true);
$error = $arr['errorDescription'];
In this case you take the json and parse it into the function json_decode of php, wit true value as second parameter It return an associative error where you can get your value by key.
DEMO
It's actually a simple matter as there are already builtin functions in PHP for handling JSON. Those are json_encode and json_decode.
$result='{"errorCode":5,"errorDescription":"Unknown account"}';
var_dump($result);
$json=json_decode($result);
var_dump($json);
echo "errorCode={$json->errorCode}<br>";
echo "errorDescription={$json->errorDescription}<br>";
Try the above. Note that json_decode can convert a string into either an object (default) or an associative array.
$json2=json_decode($result,TRUE);
var_dump($json2);
echo "errorCode=".$json2['errorCode']."<br>";
echo "errorDescription=".$json2['errorDescription']."<br>";
I have a JSON that's strangely formatted ...but it's the way I receive it. Because the arrays inside are huge, simply copying and pasting it takes a long time, so I'm wondering if there's a PHP way to do it.
The way I get it is like this:
{"count":459,"results":[{"title":"Something ....}],"params":{"limit..},"type":"Listing","pagination":{"..":5}}
But I want to get only the "results" array, basically the part of [{"title":"Something ....}]. How would I do that?
Do
$arr = json_decode(your_json, true);
If you ned it as JSON again, do
json_encode($arr['results']);
You can get to that part as follows:
$json = '{"count":459,"results":[{"title":"Something ...."}],"params":{"limit":".."},"type":"Listing","pagination":{"..":5}}';
$obj = json_decode($json);
echo $obj->results[0]->title;
Outputs:
Something ....
You have to decode your json. Be sure that the json is valid!
Your decoded json returns an object of type stdClass instead of an array!
See below the tested code:
$json = '{"count":459,"results":[{"title":"Something ...."}],"params":{"limit": "foo"},"type":"Listing","pagination":{"foo":5}}';
$decoded = json_decode($json);
So you can access array results from the object $decoded:
print_r($decoded->results);
But, in the array, there are objects, thus:
echo $decoded->results[0]->title; // Something ....
I am trying to output some json data from a json file via php but it does not seem to work. I tried this:
<?php
$jsonFile = file_get_contents('dataset/dataset.json');
$data = json_decode($jsonFile, true);
echo $data->{'data'}[0]->{'letter'}
?>
The json file is following:
{
"data":[
{
"letter":"A",
"blocks":{
"1":"0",
"2":"0",
"3":"0",
"4":"0",
"5":"0"
}
}
]}
Basically it should output the letter "A" but it outputs nothing. What did I do wrong?
Thanks
P.S. I tried to do it like here: How to process JSON in PHP? but it does not work.
After json_decode($jsonFile, true) your data is in array. So you should not access using object. Access data by array index. Try this..
echo $data['data'][0]['letter'];
More about json_decode()
This says, you get an array (the true parameter):
$data = json_decode($jsonFile, true);
You can see this if you do this:
print_r($data);
Try this:
echo $data['data'][0]['letter'];