2016-05-25 21 views
0

ich in meinem Skript folgenden curl Code bin mitCurl-Response-Echo/Shop in PHP

  $ch = curl_init(self::ONTRAPORT_API_ENDPOINT); 
      curl_setopt($ch, CURLOPT_HTTPHEADER, self::$ONTRAPORT_AUTH_HEADERS); 
      curl_setopt($ch, CURLOPT_POST, true); 
      curl_setopt($ch, CURLOPT_POSTFIELDS, $params); 
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

      $response = curl_exec($ch); 
      $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
      curl_close($ch); 

     //echo $status_code; 
      if ($status_code === 200 || $status_code === 201) { 
$jsonen = json_decode($response, true); 
       $accountId = $jsonen['id'];  // this is not working 
      } 

Was ich versuche zu tun, "id" zu erklären, dass ich in curl Antwort. Bitte überprüfen Sie den Kommentar im obigen Code. Aber ich kann die ID nicht in der Variablen $ accountId speichern.

Meine curl Antwort ist so etwas wie dies

{ 
    "code": 0, 
    "data": { 
    "firstname": "test1", 
    "lastname": "test2", 
    "email": "[email protected]", 
    "id": "39948" 
    }, 
    "updates": [], 
    "notifications": [], 
    "account_id": "26615" 
} 

Also ich brauche "39948" in meiner Variable gespeichert bekommen. Bitte beraten.

Antwort

3

JSON ist eine Datenhierarchie. Die „id“ Schlüssel ist unter „Daten“, so dass Sie es wie folgt zugreifen:

$jsonen['data']['id']; 
+0

Aber es ist ein Objekt. –

+1

@Kkinsey Nicht wenn der zweite Parameter von 'json_decode' 'wahr' ist. –

0
$the_id = $jsonen->data-id; //save the ID in a variable