0

I'm trying to echo certain value from cURL request.

My PHP code:

$curl = curl_init();

curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://api.envato.com/v1/market/new-files:themeforest,site-templates.json',
CURLOPT_HTTPHEADER => array('Authorization: Bearer myuniquekeygoeshere')
));

$resp = curl_exec($curl);
echo $resp;

curl_close($curl);

When I use echo $resp I'm getting this (this is the screenshot from their API but I'm getting the same so it works well so far): enter image description here

How can I echo certain value from this - lets say "Emanate - Startup Landing Page"?

I've tried echo $resp["new-files"][0]["item"]; but instead of name I'm getting { and nothing else.

3 Answers 3

1

The answer you are getting is a JSON. You have to do something like this:

$resp = curl_exec($curl);
$respArray = json_decode($resp, true);
$item = $respArray['new-files'][0]['item'];

Read more here

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

Comments

1

You are getting response in JSON so need to decode JSON to Array after that you can use this, try once

<?php
$curl = curl_init();

curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://api.envato.com/v1/market/new-files:themeforest,site-templates.json',
CURLOPT_HTTPHEADER => array('Authorization: Bearer myuniquekeygoeshere')
));

$resp = curl_exec($curl);
$response = json_decode($resp, true);
print_r($response);

curl_close($curl);

Comments

1

You could decode the json and access to the proper index in the array

eg for the firts occurrence of item

$my_array = json_decode($resp, true);

echo $my_array['new-files][0]['item'];

1 Comment

json_decode($resp, true); in order to have as output an associative array.

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.