I am trying to write a script that gets a JSON file from a remote location (in this case being twitch.tv) using cURL (don't think that part is too relevant, though I better mention it anyway). For example purposes, lets say the JSON object it returns looks something like this after being stored in a variable:
$json_object = {"_links":{"self":"https://api.twitch.tv/kraken/streams/gmansoliver","channel":"https://api.twitch.tv/kraken/channels/gmansoliver"},"stream":null}
I access the "stream" property, I have tried the follow code:
<?php
$json_object = {"_links":{"self":"https://api.twitch.tv/kraken/streams/gmansoliver","channel":"https://api.twitch.tv/kraken/channels/gmansoliver"},"stream":null}
$json_decoded = json_decode($json_object, true);
echo $json_decoded->stream;
?>
When I try this, I get the error "Notice: Trying to get property of non-object in D:\Servers\IIS\Sites\mysite\getstream.php on line 48".
Am I using json_decode() wrong, or is there something wrong with the JSON object I am being sent from twitch?
Edit:
I now have the JSON object:
{"access_token": "qwerty1235","refresh_token": "asdfghjkl=","scope": ["user_read"]}
If I try to decode it using json_decode() I get the following error: Object of class stdClass could not be converted to string. Any advice?
Thanks in advance for any help
You're decoding the JSON into an array. json_decode($json_object, true);
Will return an array
array (size=2)
'_links' =>
array (size=2)
'self' => string 'https://api.twitch.tv/kraken/streams/gmansoliver' (length=48)
'channel' => string 'https://api.twitch.tv/kraken/channels/gmansoliver' (length=49)
'stream' => null
If you remove the second parameter and run it as json_decode($json_object)
object(stdClass)[1]
public '_links' =>
object(stdClass)[2]
public 'self' => string 'https://api.twitch.tv/kraken/streams/gmansoliver' (length=48)
public 'channel' => string 'https://api.twitch.tv/kraken/channels/gmansoliver' (length=49)
public 'stream' => null
See the documentation, When TRUE, returned objects will be converted into associative arrays.
You have set the second parameter ($assoc) of json_decode() to true, which means it's going to return an associative array instead of an object. You then tried to reference the object style. If you are setting the second parameter to true, you need to use the associative array style to access the stream content. It would be:
$json_decoded['stream']
If you set the $assoc parameter to false (or do not specify the parameter) then you can reference it as an object:
$json_decoded->stream
If you do var_dump on the $json_decoded variable you will see what it looks like. This is a good way to see what you are working with.
Related
I am trying to write a script that gets a JSON file from a remote location (in this case being twitch.tv) using cURL (don't think that part is too relevant, though I better mention it anyway). For example purposes, lets say the JSON object it returns looks something like this after being stored in a variable:
$json_object = {"_links":{"self":"https://api.twitch.tv/kraken/streams/gmansoliver","channel":"https://api.twitch.tv/kraken/channels/gmansoliver"},"stream":null}
I access the "stream" property, I have tried the follow code:
<?php
$json_object = {"_links":{"self":"https://api.twitch.tv/kraken/streams/gmansoliver","channel":"https://api.twitch.tv/kraken/channels/gmansoliver"},"stream":null}
$json_decoded = json_decode($json_object, true);
echo $json_decoded->stream;
?>
When I try this, I get the error "Notice: Trying to get property of non-object in D:\Servers\IIS\Sites\mysite\getstream.php on line 48".
Am I using json_decode() wrong, or is there something wrong with the JSON object I am being sent from twitch?
Edit:
I now have the JSON object:
{"access_token": "qwerty1235","refresh_token": "asdfghjkl=","scope": ["user_read"]}
If I try to decode it using json_decode() I get the following error: Object of class stdClass could not be converted to string. Any advice?
Thanks in advance for any help
You're decoding the JSON into an array. json_decode($json_object, true);
Will return an array
array (size=2)
'_links' =>
array (size=2)
'self' => string 'https://api.twitch.tv/kraken/streams/gmansoliver' (length=48)
'channel' => string 'https://api.twitch.tv/kraken/channels/gmansoliver' (length=49)
'stream' => null
If you remove the second parameter and run it as json_decode($json_object)
object(stdClass)[1]
public '_links' =>
object(stdClass)[2]
public 'self' => string 'https://api.twitch.tv/kraken/streams/gmansoliver' (length=48)
public 'channel' => string 'https://api.twitch.tv/kraken/channels/gmansoliver' (length=49)
public 'stream' => null
See the documentation, When TRUE, returned objects will be converted into associative arrays.
You have set the second parameter ($assoc) of json_decode() to true, which means it's going to return an associative array instead of an object. You then tried to reference the object style. If you are setting the second parameter to true, you need to use the associative array style to access the stream content. It would be:
$json_decoded['stream']
If you set the $assoc parameter to false (or do not specify the parameter) then you can reference it as an object:
$json_decoded->stream
If you do var_dump on the $json_decoded variable you will see what it looks like. This is a good way to see what you are working with.
In php 7.4 strict mode I have this error
PHP Notice: Trying to access array offset on value of type bool in
I do not understand exactly the problem
Thank you
public array $billing;
the line where the error appear :
$this->billing = [
'firstname' => $billing_address['entry_firstname'],
'state' => (!is_null($billing_address['entry_state']) ? $billing_address['entry_state'] : $billing_address['zone_name']),
'zone_id' => $billing_address['entry_zone_id'],
'country' => array('id' => $billing_address['countries_id'], 'title' => $billing_address['countries_name'], 'iso_code_2' => $billing_address['countries_iso_code_2'], 'iso_code_3' => $billing_address['countries_iso_code_3']),
];
The error means that you use a variable as array, but actually it is bool.
I guess that $billing_address is false instead of array.
It got false while you were trying to fetch if from DB or other source.
So you should always check if you have a value there and put a default value like empty array []. Or throw an exception "billing address not found".
i have a problem about php json_decode, now i have a json string like this
"{\"client_id\":\"yunniao\",\"client_secret\":\"sdfW234eDtjkuDFityDu345JYyuk5Jom\",\"insurance_applicant_id\":1,\"policyholder\":\"\\u5317\\u4eac\\u4e91\\u4e2d\\u5c0f\\u9e1f\\u79d1\\u6280\\u6709\\u9650\\u516c\\u53f8\",\"ower_name\":\"\\u8bd7\\u6768\\u8fd0\\u529b\\u4e8c\",\"plate_no\":\"\\u4eacM00002\",\"trade_no\":\"2932848\",\"cargo_type\":\"\\u4e34\\u65f6\\u4efb\\u52a1-\\u6709SOP\",\"cargo_amount\":\"1-1\",\"cargo_number_min\":1,\"cargo_number_max\":1,\"start_place\":\"\\u5367\\u9f99\\u5c0f\\u533a\",\"end_place\":\"\\u4e3b\\u4efb\\u52a1-\\u6709SOP\",\"effect_date\":\"2017-07-01T00:08:44+08:00\",\"coverage\":100000,\"freight\":\"3000\"}"
is a very json string,;
i put this in Laravel artisan tiker the above string return me like this, it's a image, but it's not show in here:
but in the php interactive command line, the json string give me nothing, the result like behind image;
i'm tried a lot of possibilities;
i tried to put above code in my Laravel project, then test the file, it give me null;
i googled so many times, but there is no answer;
it's all failed, now i'm here, i think i want help!
thanks for any help and workround.
===================================================================
Hello guys, i have been resolved this, i used eval, the eval like this
$param = "{\"insurance_applicant_id\":1,\"plate_no\":\"\\u4eacA11113\",\"trade_no\":86254,\"cargo_type\":\"\",\"cargo_number_min\":0,\"cargo_number_max\":0,\"cargo_amount\":\"0~0\",\"start_place\":\"\\u897f\\u82d1\",\"end_place\":\"\\u914d\\u9001\\u533a\\u57df\\u63cf\\u8ff0\",\"effect_date\":\"2016-01-09 14:54:04\",\"coverage\":0,\"freight\":79200}";
eval("\$param = \"$param\";");
$decodeValue = json_decode($param);
hope this help for other people, i closed this, thanks everybody again!
The interactive PHP console simply does not automatically output the value of the last evaluated expression. Your PHP code doesn't echo anything, so there's no output. You need to print_r(json_decode(...)) to see something.
Try this
$json = "{\"client_id\":\"yunniao\",\"client_secret\":\"sdfW234eDtjkuDFityDu345JYyuk5Jom\",\"insurance_applicant_id\":1,\"policyholder\":\"\\u5317\\u4eac\\u4e91\\u4e2d\\u5c0f\\u9e1f\\u79d1\\u6280\\u6709\\u9650\\u516c\\u53f8\",\"ower_name\":\"\\u8bd7\\u6768\\u8fd0\\u529b\\u4e8c\",\"plate_no\":\"\\u4eacM00002\",\"trade_no\":\"2932848\",\"cargo_type\":\"\\u4e34\\u65f6\\u4efb\\u52a1-\\u6709SOP\",\"cargo_amount\":\"1-1\",\"cargo_number_min\":1,\"cargo_number_max\":1,\"start_place\":\"\\u5367\\u9f99\\u5c0f\\u533a\",\"end_place\":\"\\u4e3b\\u4efb\\u52a1-\\u6709SOP\",\"effect_date\":\"2017-07-01T00:08:44+08:00\",\"coverage\":100000,\"freight\":\"3000\"}";
$decoded = json_decode($json, true);
It will give you decoded json in array form like this
[
"client_id" => "yunniao",
"client_secret" => "sdfW234eDtjkuDFityDu345JYyuk5Jom",
"insurance_applicant_id" => 1,
"policyholder" => "北京云中小鸟科技有限公司",
"ower_name" => "诗杨运力二",
"plate_no" => "京M00002",
"trade_no" => "2932848",
"cargo_type" => "临时任务-有SOP",
"cargo_amount" => "1-1",
"cargo_number_min" => 1,
"cargo_number_max" => 1,
"start_place" => "卧龙小区",
"end_place" => "主任务-有SOP",
"effect_date" => "2017-07-01T00:08:44+08:00",
"coverage" => 100000,
"freight" => "3000",
]
I'm trying to get JSON data back from the Data Science Toolkit, but the response I am getting doesn't have it. I am trying to pass a string object (json encoded from an array of addresses) to the dstk field, to return a list of coordinates.
Here's the following code snippet:
$client = new \GuzzleHttp\Client(['base_uri' => 'http://www.datasciencetoolkit.org/']);
$request = $client->post('street2coordinates', ['form_params' => ['body' => json_encode($addresses)] ]);
And when I die dump
dd($request->getBody()),it just returns the ff object. or even just $request, I can't pull json data on my search I am expecting Latitude/Longitude coordinates - but I have no idea how/where to get them.
object(GuzzleHttp\Psr7\Stream)[215]
private 'stream' => resource(8, stream)
private 'size' => null
private 'seekable' => boolean true
private 'readable' => boolean true
private 'writable' => boolean true
private 'uri' => string 'php://temp' (length=10)
private 'customMetadata' =>
array (size=0)
empty
I have tried using the Goutte client as well, to no avail. Even trying different methods like ->json() or ->toJson(), no dice. Help is appreciated. Thanks!
You have to force the body to a string, for example by echo'ing it, or casting it explicitly.
echo $response->getBody();
Or
dd((string)$response->getBody());
I am trying to write a script that gets a JSON file from a remote location (in this case being twitch.tv) using cURL (don't think that part is too relevant, though I better mention it anyway). For example purposes, lets say the JSON object it returns looks something like this after being stored in a variable:
$json_object = {"_links":{"self":"https://api.twitch.tv/kraken/streams/gmansoliver","channel":"https://api.twitch.tv/kraken/channels/gmansoliver"},"stream":null}
I access the "stream" property, I have tried the follow code:
<?php
$json_object = {"_links":{"self":"https://api.twitch.tv/kraken/streams/gmansoliver","channel":"https://api.twitch.tv/kraken/channels/gmansoliver"},"stream":null}
$json_decoded = json_decode($json_object, true);
echo $json_decoded->stream;
?>
When I try this, I get the error "Notice: Trying to get property of non-object in D:\Servers\IIS\Sites\mysite\getstream.php on line 48".
Am I using json_decode() wrong, or is there something wrong with the JSON object I am being sent from twitch?
Edit:
I now have the JSON object:
{"access_token": "qwerty1235","refresh_token": "asdfghjkl=","scope": ["user_read"]}
If I try to decode it using json_decode() I get the following error: Object of class stdClass could not be converted to string. Any advice?
Thanks in advance for any help
You're decoding the JSON into an array. json_decode($json_object, true);
Will return an array
array (size=2)
'_links' =>
array (size=2)
'self' => string 'https://api.twitch.tv/kraken/streams/gmansoliver' (length=48)
'channel' => string 'https://api.twitch.tv/kraken/channels/gmansoliver' (length=49)
'stream' => null
If you remove the second parameter and run it as json_decode($json_object)
object(stdClass)[1]
public '_links' =>
object(stdClass)[2]
public 'self' => string 'https://api.twitch.tv/kraken/streams/gmansoliver' (length=48)
public 'channel' => string 'https://api.twitch.tv/kraken/channels/gmansoliver' (length=49)
public 'stream' => null
See the documentation, When TRUE, returned objects will be converted into associative arrays.
You have set the second parameter ($assoc) of json_decode() to true, which means it's going to return an associative array instead of an object. You then tried to reference the object style. If you are setting the second parameter to true, you need to use the associative array style to access the stream content. It would be:
$json_decoded['stream']
If you set the $assoc parameter to false (or do not specify the parameter) then you can reference it as an object:
$json_decoded->stream
If you do var_dump on the $json_decoded variable you will see what it looks like. This is a good way to see what you are working with.