2017-03-21 94 views
0

幫助,我找不到解決方案在這裏,搜索沒有幫助,所以我決定在這裏發佈一個問題。如何獲取對象內的數據:列表,數據

<?php 
function httpGet($url) 
{ 
    $ch = curl_init(); 

$headers = array('Authorization: Bearer xxxxxxxxxxx'); 
    curl_setopt($ch, CURLOPT_URL,$url); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 

    $output=curl_exec($ch); 

    curl_close($ch); 
    $output = json_encode($output,true); 
    $output = json_decode($output,true); 
    return $output; 
} 

$a = httpGet("https://api.stripe.com/v1/customers"); 

echo'<pre>'; 
print_r($a); 

?> 

我的問題是如何獲得ID? 我只想要ID。我怎樣才能訪問它? 我也覺得他們的代碼有問題,請幫忙解決。

{ 
"object": "list", 
"data": [ 
    { 
    "id": "cus_AKIT4Rz6NHlKMa", 
    "object": "customer", 
    "account_balance": 0, 
    "created": 1490067687, 
    "currency": null, 
    "default_source": null, 
    "delinquent": false, 
    "description": null, 
    "discount": null, 
    "email": "[email protected]", 
    "livemode": false, 
    "metadata": {}, 
    "shipping": null, 
    "sources": { 
     "object": "list", 
     "data": [], 
     "has_more": false, 
     "total_count": 0, 
     "url": "/v1/customers/cus_AKIT4Rz6NHlKMa/sources" 
    }, 
    "subscriptions": { 
     "object": "list", 
     "data": [], 
     "has_more": false, 
     "total_count": 0, 
     "url": "/v1/customers/cus_AKIT4Rz6NHlKMa/subscriptions" 
    } 
    }, 
    { 
    "id": "cus_AKIP3muBIL95H0", 
    "object": "customer", 
    "account_balance": 0, 
    "created": 1490067468, 
    "currency": null, 
    "default_source": null, 
    "delinquent": false, 
    "description": null, 
    "discount": null, 
    "email": null, 
    "livemode": false, 
    "metadata": {}, 
    "shipping": null, 
    "sources": { 
     "object": "list", 
     "data": [], 
     "has_more": false, 
     "total_count": 0, 
     "url": "/v1/customers/cus_AKIP3muBIL95H0/sources" 
    }, 
    "subscriptions": { 
     "object": "list", 
     "data": [], 
     "has_more": false, 
     "total_count": 0, 
     "url": "/v1/customers/cus_AKIP3muBIL95H0/subscriptions" 
    } 
    }, 

刪除json_encode($ output,true);結果是

Array 
(
    [object] => list 
    [data] => Array 
     (
      [0] => Array 
       (
        [id] => cus_AKIT4Rz6NHlKMa 
        [object] => customer 
        [account_balance] => 0 
        [created] => 1490067687 
        [currency] => 
        [default_source] => 
        [delinquent] => 
        [description] => 
        [discount] => 
        [email] => [email protected] 
        [livemode] => 
        [metadata] => Array 
         (
         ) 

        [shipping] => 
        [sources] => Array 
         (
          [object] => list 
          [data] => Array 
           (
           ) 

          [has_more] => 
          [total_count] => 0 
          [url] => /v1/customers/cus_AKIT4Rz6NHlKMa/sources 
         ) 

        [subscriptions] => Array 
         (
          [object] => list 
          [data] => Array 
           (
           ) 

          [has_more] => 
          [total_count] => 0 
          [url] => /v1/customers/cus_AKIT4Rz6NHlKMa/subscriptions 
         ) 

       ) 
+0

用於獲取標識使用'回聲$一[ 「數據」] [0] [ID];使用數據' –

回答

0

進入您的ID像這樣...

function httpGet($url) { 
    $ch = curl_init(); 
    $headers = array('Authorization: Bearer xxxxxxxxxxx'); 
    curl_setopt($ch, CURLOPT_URL,$url); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    $output=curl_exec($ch); 
    curl_close($ch); 
    // $output = json_decode($output,true); only necessary if the result of $output is a json encoded array 
    return $output; 
} 

$array = httpGet("https://api.stripe.com/v1/customers"); 

for($i = 0; $i < count($array["data"]); $++){ 

    echo $array["data"][$i]["id"] . "\r\n"; 

} 
+0

林作爲PHP。 –

+0

結果顯示在我的帖子上,在我的代碼下方顯示在print_r($ a)之後。 –

+0

這顯示了你編碼後,然後決定它的結果。我想看看$輸出的結果,你這樣做 –