阅读Json Curl内容导致php

时间:2017-12-03 11:41:00

标签: php json curl

我有一个json输出,我试图获取其内容

GreenArea

以下是我在PHP中使用的Json Curl

UIView

我的成功 我能够为每个循环获取id Ruslan's的所有值。

我的问题

1。)我无法分别为每个循环获取地址位置@IBOutlet weak var greenAreaVw: UIView! var contHeight : CGFloat = 0.0 var eachRowHeight : CGFloat = 45 var topSpaceTableView : CGFloat = 62 var GreenAreaOriginY : CGFloat = 0.0 // Give UITableView Edge Insets in ViewDidLoad contHeight = ((self.view.frame.size.height / 2) - eachRowHeight / 2 - topSpaceTableView) userTblVw.contentInset = UIEdgeInsets(top: contHeight, left: 0, bottom: contHeight, right: 0) userTblVw.contentOffset.y = -contHeight GreenAreaOriginY = greenAreaVw.frame.origin.y /*------------------- -----------------------*/ func scrollViewDidEndDecelerating(scrollView: UIScrollView) { checkCells() } func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) { checkCells() } func checkCells() { userTblVw.visibleCells.forEach { cell in if let indexPath = userTblVw.indexPathForCell(cell) { let rect = userTblVw.rectForRowAtIndexPath(indexPath) let convertedRect = self.userTblVw.convertRect(rect, toView: self.view) if convertedRect.origin.y >= GreenAreaOriginY && convertedRect.origin.y < (GreenAreaOriginY + eachRowHeight) { let contFloat : CGFloat = (eachRowHeight * CGFloat(indexPath.row)) - contHeight userTblVw.setContentOffset(CGPoint(x: 0, y: contFloat), animated: true) } } } } 和电话类型{"content":"people", "nextLink":"https://example.com/people?$skip=3", "value":[ { "id":"100","displayName":"Room Rainier", "Addresses":[{"location":"12 orlando street","Spore":8.0}], "phones":[{"type":"home","number":"10000000000"}], "personType":{"class":"new","subclass":"Room1"} }, { "id":"102","displayName":"Tony Blur", "Addresses":[{"location":"19 saco street","Spore":4.0}], "phones":[{"type":"business","number":"1080000000"}], "personType":{"class":"Other","subclass":"Room2"} }, { "id":"103","displayName":"veronica huges", "Addresses":[{"location":"6 nano street","Spore":7.0}], "phones":[{"type":"business","number":"111000000"}], "personType":{"class":"old","subclass":"Room5"} }]} 的值,因为它没有显示任何内容。

2.)json文件因此具有下一个链接选项 的 <?php session_start(); $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => "https://example.com/people", CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => "", CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 30, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, //CURLOPT_CUSTOMREQUEST => "GET", //CURLOPT_POSTFIELDS => "$data", CURLOPT_HTTPHEADER => array( "authorization: Bearer --my bearer goes here--" ), )); $response = curl_exec($curl); $err = curl_error($curl); curl_close($curl); $res=json_decode($response); $res1=json_decode($response, true); $js = json_decode($response, true); echo '<pre>' . print_r($response, true) . '</pre>'; $re = $js['value']; foreach ($re as $value1) { echo $value1['id']; echo '<br>'; echo $value1['Addresses']['location']; echo '<br>'; echo $value1['phones']['type']; echo '<br>'; } if ($err) { echo "cURL Error #:" . $err; } else { //echo $response; } ?>

如何根据链接显示更多用户/人员数据。感谢

1 个答案:

答案 0 :(得分:1)

地址和电话是阵列,所以如果你想获得第一个,你可以使用索引0,或者如果有超过1个地址或电话,你可以全部使用循环。

foreach ($re as $value1) {
    /**
     * To get the first address and phone use index 0
     */
    echo $value1['Addresses'][0]['location'];
    echo $value1['phones'][0]['type'];

    /**
     * Loop to get all addresses and phones
     */
    foreach($value1['Addresses'] as $address) {
        echo $address['location'];
    }

    foreach($value1['phones'] as $phone) {
        echo $phone['type'];
    }
}

如果有下一个链接,您应该请求nextLink再次使用curl和下一个链接。

如果为此创建一个函数并且只需使用下一个链接再次调用该函数,将会更容易。