0

Now, I use strstr to get data from external JSON file. I'm not sure if it's the fastest way to do that what I want and I can't test because json_decode don't work in my code.

$before = '"THIS":"';
$after = '","date"';
$data = strstr(substr($url, strpos($url, $before) + strlen($before)), $after, true)

and with json_decode:

$address = file_get_contents('http://json.link/?something=Hello');
$data = json_decode($address);
echo $data->data->THIS;

Now, when I replace my first code with second I get:

Notice: Trying to get property of non-object

All my code:

$text = "a lot of text";
$text_split = array(0 => '');
$number = 0;
$words = explode(' ', $text);
foreach($words as $word)
{
    if(strlen($text_split[$number]) + strlen($word) + 1 > 500)
    {
        ++$number;
        $text_split[$number] = '';
    }
    $text_split[$number] .= $word.' ';
}
foreach($text_split as $texts)
{
    $text_encode = rawurlencode($texts);
    $address = file_get_contents('http://json.link/?something='.$text_encode);
    $data = json_decode($address);
    echo $data->data->THIS;
}

What should in do in that case? Keep using strstr or replace all code to work with json_decode (maybe because execution time is faster?)? If the second option, how I can make json_decode work here? Thanks!

... and sorry for bad english.

LE: If I replace $address = file_get_contents('http://json.link/?something='.$text_encode); with $address = file_get_contents('http://json.link/?something=Hello'); I get VALID result for "Hello" text but 10 times. I guess because it's in a foreach.

Kratalin Jan
  • 105
  • 10

2 Answers2

0

json_decode is the suggested method to work with JSON data. Here I think you are trying to access an invalid property in JSON object.

$data = json_decode($address);
echo $data->data->THIS;

I guess you need $data->date instead of $data-data?

mesutozer
  • 2,839
  • 1
  • 12
  • 13
0

you have to access the specific key value like this

$json = '{"success":true,"msg":"success","data":{"THIS":"thing I need","date":"24.03.2014","https":false}}';

$d=json_decode($json,true);

echo $d['data']['THIS'];
ɹɐqʞɐ zoɹǝɟ
  • 4,342
  • 3
  • 22
  • 35