如何获取对象内的数据:列表,数据

时间:2017-03-21 05:24:15

标签: php curl

帮助,我无法在此找到解决方案,搜索没有帮助,所以我决定在这里发帖。

<?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? 我只想获得身份证。我该如何访问它? 我也觉得他们的代码有问题,请帮助并纠正它。

{
"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": "hello@gmail.com",
    "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] => hello@gmail.com
                    [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
                        )

                )

1 个答案:

答案 0 :(得分:0)

像这样接受你的身份......

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";

}