0

I am using the Mavenlink API to create a project:

$response = $client->createWorkspace(array("title" => $project_number . ' ' . $company_name, "creator_role" => "maven"));

print_r($response);

$project_id = $response->id;

echo "project id: " . $project_id;

The part of creating the project is working fine, but I'm having trouble finding the correct path to "id". I want the ID so I can then use the update method to add more information to the project.

Heres a screenshot of the response output:

enter image description here

Raw response:

{"count":1,"workspaces":{"12220207":{"title":"34343 Add New","archived":false,"description":"","due_date":"","effective_due_date":"","start_date":"","budgeted":true,"change_orders_enabled":true,"updated_at":"2016-05-12T10:51:01-07:00","created_at":"2016-05-12T10:51:01-07:00","consultant_role_name":"FUEL","client_role_name":"Clients","percentage_complete":0,"access_level":"open","exclude_archived_stories_percent_complete":false,"can_create_line_items":true,"default_rate":"125.00","currency_symbol":"$","currency_base_unit":100,"can_invite":true,"has_budget_access":true,"tasks_default_non_billable":false,"rate_card_id":null,"workspace_invoice_preference_id":null,"posts_require_privacy_decision":false,"require_time_approvals":false,"require_expense_approvals":false,"stories_are_fixed_fee_by_default":false,"price":"TBD","price_in_cents":null,"budget_used":"$0","over_budget":false,"currency":"USD","expenses_in_burn_rate":true,"budget_used_in_cents":0,"total_expenses_in_cents":0,"status":{"color":"grey","message":"Not Started","key":130},"permissions":{"can_upload_files":true,"can_private_message":true,"can_join":false,"is_participant":true,"access_level":"admin","team_lead":true,"user_is_client":false},"creator_id":"10340340030","id":"12220207"}},"results":[{"key":"workspaces","id":"12220207"}]}

When I echo project_id, it is displaying blank. What is the correct path to target "id" ?

Thanks!

cpcdev
  • 1,130
  • 3
  • 18
  • 45

3 Answers3

1

Just json decode and you will get it. Let say you have $json your json.

Online check

$arr = json_decode($json);    
echo $arr->workspaces->{12220207}->id; //12220207

For getting the results id

echo $arr->results[0]->id; //12220207
Murad Hasan
  • 9,565
  • 2
  • 21
  • 42
  • Im confused.. in the code you provided you are still supplying the id? that is what I dont know and am trying to extract – cpcdev May 12 '16 at 17:57
  • please check the online check. You have that id in your `json` as a key. – Murad Hasan May 12 '16 at 17:58
  • 1
    Awesome.. didnt see your edit! Thanks! Works great! – cpcdev May 12 '16 at 17:58
  • @cpcdev, never forgot to mark this as useful. – Murad Hasan May 12 '16 at 18:03
  • in your first example you use the value that you are searching for. `echo $arr->workspaces->{12220207}->id; //12220207` how someone is supposed to know the id that he is searching, since that might change? –  May 12 '16 at 18:06
  • Did you see the decode of the `json` string??? First check the array and then let me know what i did? – Murad Hasan May 12 '16 at 18:07
1

You need to decode the json first using json_decode.

$response = json_decode($raw);

You could print it out so you can see the layout using print_r.

print_r($response);

As you can see it makes it an object, so you can easily access the information. The ID is the same under both results and inside workspaces. Therefore you have 2 solutions.

Either

$id = $response->results[0]->id;

Or

$id = $response->workspaces->{12220207}->id;

Which is obviously worse since the 12220207 is not a static number.

GunniH
  • 186
  • 1
  • 8
  • 12220207 is the id that is being searched so basically `$response->workspaces->{12220207}->id;` will return 12220207. –  May 12 '16 at 18:02
  • @PeterDarmis, from where you got this information that OP search with `12220207`? – Murad Hasan May 12 '16 at 18:04
  • @PeterDarmis so next time, he changes his parameters (the title and creator role) and he'll more than likely get a different workspace, with a different ID. He's not searching for that ID, he's getting it as a response. Pretty silly downvote. – GunniH May 12 '16 at 18:48
  • i simply post the question... `title - > finding the correct path to “id” in this array`. I am using the Mavenlink API to create a project... The part of creating the project is working fine, but I'm having trouble finding the correct path to "id". I want the ID so I can then use the update method to add more information to the project.... When I echo project_id, it is displaying blank. What is the correct path to target "id" ? Thanks! Can you please read? @GunniH –  May 12 '16 at 19:32
0
$json = '{"count":1,"workspaces":{"12220207":{"title":"34343 Add New","archived":false,"description":"","due_date":"","effective_due_date":"","start_date":"","budgeted":true,"change_orders_enabled":true,"updated_at":"2016-05-12T10:51:01-07:00","created_at":"2016-05-12T10:51:01-07:00","consultant_role_name":"FUEL","client_role_name":"Clients","percentage_complete":0,"access_level":"open","exclude_archived_stories_percent_complete":false,"can_create_line_items":true,"default_rate":"125.00","currency_symbol":"$","currency_base_unit":100,"can_invite":true,"has_budget_access":true,"tasks_default_non_billable":false,"rate_card_id":null,"workspace_invoice_preference_id":null,"posts_require_privacy_decision":false,"require_time_approvals":false,"require_expense_approvals":false,"stories_are_fixed_fee_by_default":false,"price":"TBD","price_in_cents":null,"budget_used":"$0","over_budget":false,"currency":"USD","expenses_in_burn_rate":true,"budget_used_in_cents":0,"total_expenses_in_cents":0,"status":{"color":"grey","message":"Not Started","key":130},"permissions":{"can_upload_files":true,"can_private_message":true,"can_join":false,"is_participant":true,"access_level":"admin","team_lead":true,"user_is_client":false},"creator_id":"6333747","id":"12220207"}},"results":[{"key":"workspaces","id":"12220207"}]}';
$result = json_decode($json,true);    
//print_r($result);
echo $result['results'][0]['id'];

http://sandbox.onlinephpfunctions.com/code/27d4ff8d1632177db97f9e9122e7a51bd6e11146