0

I have a REST API that returns a list of tasks in JSON Format, I decode it into an array using :

$decoded = json_decode($curl_response,true); 

and am having trouble in using that array.

EDIT : Sorry for modifying question but the curl_response is

{
    "error": true,
    "message": "Api key is misssing"
}

When I login there is an API key in JSON response, I don't know how to use further. That key is required for authentication in any further call of API. How to include that apikey in my request header for further curl calls?

The response is of the form (as tested using Advanced REST Client in Chrome):

{
    error: false
    tasks: [5]
        0:  {
            id: 2
            task: "[email protected]"
            status: 0
            createdAt: "2014-10-21 21:42:48"
        }-
        1:  {
            id: 3
            task: "Inter Nam"
            status: 0
            createdAt: "2014-10-21 21:42:58"
        }-
        2:  {
            id: 4
            task: "Vamos"
            status: 0
            createdAt: "2014-10-21 21:43:04"
        }-
        3:  {
            id: 5
            task: "El Mundo"
            status: 0
            createdAt: "2014-10-21 23:12:33"
        }-
        4:  {
            id: 6
            task: "El Clasico"
            status: 0
            createdAt: "2014-10-21 23:12:45"
        }-
    -
}

I tried to decode the above response in an array $decoded and then I want to display each task in a loop, get total no. of tasks..etc. --------------------------------(1) Using Count($decoded) I get 2 which is clearly wrong, also I am unable to to get the individual tasks using :

foreach($decoded as $task) {
    echo $task["task"];
}

How to implement (1) ?

7
  • You need to open up the tasks item; foreach ($decoded['tasks'] as $task). Commented Oct 21, 2014 at 18:11
  • Post the raw responce from your CURL call, as that data structure is not valid JSON which might be the reason you are having a problem, or you have changed it somehow before pasting in in your question Commented Oct 21, 2014 at 18:16
  • @RiggsFolly How to get the raw reponse from CURL call ? Commented Oct 21, 2014 at 18:20
  • Just echo $curl_response Commented Oct 21, 2014 at 18:41
  • @RiggsFolly Thanks. I did that and now I have to send api key as a request header in curl call. So, as said i just append the api key at the end of url or is there any other way too? Commented Oct 21, 2014 at 18:46

1 Answer 1

0

There is another level above what are you trying to access. Try this:

$tasks = $decoded['tasks'];
foreach($tasks as $task) {
    echo $task["task"];
}
Sign up to request clarification or add additional context in comments.

12 Comments

I am sure you must be right but It isn't working for me.
I get no results . How can I check if my curl response is correct ?
Hmm you already said it was ok. You could just echo out $curl_response?
I thought I would need some function.... yes it is wrong {"error":true,"message":"Api key is misssing"}
I am sorry but can you tell how to include api key in the request header in Curl ?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.