0

I am very new to json in combination with php and wonder why the following code is not working. My mission is displaying the title items one by one.

<?php
$json_string =    file_get_contents("https://www.fiverr.com/gigs/gigs_as_json?host=search&type=single_query&query_string=pet&search_filter=rating&category_id=3&sub_category_id=49&limit=48");
$parsed_json = json_decode($json_string, true);
$parsed_json = $parsed_json['gigs'];

foreach($parsed_json as $key => $value)
{
   echo $value['title'] . '<br>';
}
?>

The next issue will by, how would I be able to save the image item called cloud_img_med to my server into /grabbed

Your help would be greatly appreciated.

3 Answers 3

1

To answer your question about why this code isn't working:

I tried to set it up myself, and found out that the output fetched by file_get_contents is compressed. You can see that by reading the output in a simple var_dump:

var_dump($json_string);

To fix this, you'll need to use a custom context with no compression:

$context = stream_context_create(
    array(
        'http' => array(
            'method' => "GET",
            'header' => "Accept-Encoding: gzip;q=0, compress;q=0\r\n",
        )
));

Then pass it as the third parameter:

$json_string = file_get_contents("https://www.fiverr.com/gigs/gigs_as_json?host=search&type=single_query&query_string=pet&search_filter=rating&category_id=3&sub_category_id=49&limit=48", false, $context);

I should do this JSON string readable

Sign up to request clarification or add additional context in comments.

Comments

0

The code seems to work fine - I tried it on my local machine.

Running this

<?php
$str = <<<EOT
// JSON here...
EOT;

$parsed_json = json_decode($str, true);
$parsed_json = $parsed_json['gigs'];

foreach($parsed_json as $key => $value)
{
   echo $value['title'] . '<br>';
}
?>

Gives me this output

replace the MGM lion with your pet or whoever<br>design creative Animal And Pet Company logo<br>`

Which is sort of what you want? If you're having trouble, maybe file_get_contents isn't allowed on your server and you can't get the JSON. Try hardcoding it into the $str variable like I did, and see if you get what you want.

With regards to saving the image, you can check this answer here: https://stackoverflow.com/a/724449/4396258

1 Comment

Hmm nice observation, I'll check out the file_get_contents permissions on my server :)
0

I think the problem might be the data you're getting from Fiverr's JSON API. At least in my test, I found it was using gzip encoding to compress the data you're requesting. So you have to uncompress it.

$json_string = file_get_contents("https://www.fiverr.com/gigs/gigs_as_json?host=search&type=single_query&query_string=pet&search_filter=rating&category_id=3&sub_category_id=49&limit=48");
$content = json_decode(gzinflate( substr($json_string,10,-8) ));
$gigs = $content->gigs;
foreach($gigs as $key => $value)
{
   echo $value->title . '<br>';
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.