codeigniter curl loop over json response

时间:2017-05-29 23:03:12

标签: php json codeigniter curl

我正在使用codeigniter并且正在调用curl请求并获取我无法循环的json结果。我一直在搜索并至少得到结果来显示第一个公司名称和ID,但它不允许它遍历所有名称。

我的curl_model.php

<?php


class Curl_model extends CI_model {

  public function __construct()
    {
    // created the construct so that the helpers, libraries, models can be loaded all through this controller
    parent::__construct();
    $this->load->helper('url');
    $this->load->library('xmlrpc');
    $this->load->library('xmlrpcs');
        $this->refcode = $this->session->userdata('refcode');
    }




  public function curl_index()
    {
        $app_key = "xxx";// authentication
        $user_key =  "yyy"; // authentication

      $url = "https://siteurl/api/companies";

      // append the header putting the secret key and hash

      $request_headers = array('Content-type: application/json', "X-App-Key: $app_key", "X-User-Key: $user_key" ) ;
    //  $request_headers[] = 'Authorization: Bearer ' . $secretKey;
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_TIMEOUT, 60);
      curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
      $data = curl_exec($ch);

      if (curl_errno($ch))
        {
        print "Error: " . curl_error($ch);
        }
        else
        {
        // Show me the result

        $transaction = json_decode($data, TRUE);

        curl_close($ch);

return $transaction;
        //var_dump($transaction['data']);

      }
    }



}

?>

My Curl.php控制器:

    <?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Curl extends CI_Controller {


    public function index() {
        $data['companies'] = $this->curl_model->curl_index();
        print_r($data['companies']);
        //print_r($data['companies']);

    //  foreach ($data['companies'] as $company) {
 //    echo "<br /><br /><strong>".$company['name']."</strong><br /><br />";
    // $company_id = $company['id']; 


    // } 

        $data['main_view'] = "companies/display";


        $this->load->view('layouts/main', $data);

}

    } ?>

我尝试在视图中循环作为对象但是出现错误,当我使用数组时,我得到一行输出。我试过i ++来增加$ company [$ i] [&#39; id&#39;];但仍然只有一行。这是循环

   <?php foreach($companies as $company):?>
<li>
<?php echo base_url();?>tasks/display/<?php echo $company['id']; ?>
                    <?php echo $company['name']; ?>
</li>
<?php endforeach; ?>

这是json响应在print_r中吐出($ data [&#39;公司&#39;]);

array(1) {
  ["companies"]=>
  array(20) {
    [0]=>
    array(6) {
      ["resource"]=>
      string(41) "https://apiurl.com/api/companies/85215"
      ["id"]=>
      int(85215)
      ["name"]=>
      string(28) "siteName"
      ["website"]=>
      string(0) ""
      ["resources"]=>
      array(1) {
        [0]=>
        array(1) {
          ["clients"]=>
          string(49) "https://apiurl.com/api/companies/85215/clients"
        }
      }
      ["actions"]=>
      array(1) {
        [0]=>
        array(1) {
          ["archive"]=>
          array(2) {
            ["href"]=>
            string(49) "https://apiurl.com/api/companies/85215/archive"
            ["method"]=>
            string(4) "POST"
          }
        }
      }
    }
    [1]=>
    array(6) {
      ["resource"]=>
      string(41) "https://apiurl.com/api/companies/72813"
      ["id"]=>
      int(72813)
      ["name"]=>
      string(8) "siteName "
      ["website"]=>
      string(24) "http://siteURL.com"
      ["resources"]=>
      array(1) {
        [0]=>
        array(1) {
          ["clients"]=>
          string(49) "https://apiurl.com/api/companies/72813/clients"
        }
      }
      ["actions"]=>
      array(1) {
        [0]=>
        array(1) {
          ["archive"]=>
          array(2) {
            ["href"]=>
            string(49) "https://apiurl.com/api/companies/72813/archive"
            ["method"]=>
            string(4) "POST"
          }
        }
      }
    }
    [2]=>
    array(6) {
      ["resource"]=>
      string(41) "https://apiurl.com/api/companies/73464"
      ["id"]=>
      int(73464)
      ["name"]=>
      string(10) "siteName"
      ["website"]=>
      string(25) "http://www.siteURL.com"
      ["resources"]=>
      array(1) {
        [0]=>
        array(1) {
          ["clients"]=>
          string(49) "https://apiurl.com/api/companies/73464/clients"
        }
      }
      ["actions"]=>
      array(1) {
        [0]=>
        array(1) {
          ["archive"]=>
          array(2) {
            ["href"]=>
            string(49) "https://apiurl.com/api/companies/73464/archive"
            ["method"]=>
            string(4) "POST"
          }
        }
      }
    }
    [3]=>
    array(6) {
      ["resource"]=>
      string(41) "https://apiurl.com/api/companies/46221"
      ["id"]=>
      int(46221)
      ["name"]=>
      string(9) "siteName"
      ["website"]=>
      string(0) ""
      ["resources"]=>
      array(1) {
        [0]=>
        array(1) {
          ["clients"]=>
          string(49) "https://apiurl.com/api/companies/46221/clients"
        }
      }
      ["actions"]=>
      array(1) {
        [0]=>
        array(1) {
          ["archive"]=>
          array(2) {
            ["href"]=>
            string(49) "https://apiurl.com/api/companies/46221/archive"
            ["method"]=>
            string(4) "POST"
          }
        }
      }
    }
    [4]=>
    array(6) {
      ["resource"]=>
      string(41) "https://apiurl.com/api/companies/76764"
      ["id"]=>
      int(76764)
      ["name"]=>
      string(9) "siteName"
      ["website"]=>
      string(20) "http://siteURL.com"
      ["resources"]=>
      array(1) {
        [0]=>
        array(1) {
          ["clients"]=>
          string(49) "https://apiurl.com/api/companies/76764/clients"
        }
      }
      ["actions"]=>
      array(1) {
        [0]=>
        array(1) {
          ["archive"]=>
          array(2) {
            ["href"]=>
            string(49) "https://apiurl.com/api/companies/76764/archive"
            ["method"]=>
            string(4) "POST"
          }
        }
      }
    }
    [5]=>
    array(6) {
      ["resource"]=>
      string(41) "https://apiurl.com/api/companies/76832"
      ["id"]=>
      int(76832)
      ["name"]=>
      string(8) "siteName"
      ["website"]=>
      string(32) "http://www.siteURL.com"
      ["resources"]=>
      array(1) {
        [0]=>
        array(1) {
          ["clients"]=>
          string(49) "https://apiurl.com/api/companies/76832/clients"
        }
      }
      ["actions"]=>
      array(1) {
        [0]=>
        array(1) {
          ["archive"]=>
          array(2) {
            ["href"]=>
            string(49) "https://apiurl.com/api/companies/76832/archive"
            ["method"]=>
            string(4) "POST"
          }
        }
      }
    }
    [6]=>
    array(6) {
      ["resource"]=>
      string(41) "https://apiurl.com/api/companies/93676"
      ["id"]=>
      int(93676)
      ["name"]=>
      string(16) "siteName"
      ["website"]=>
      string(23) "http://www.siteURL.com"
      ["resources"]=>
      array(1) {
        [0]=>
        array(1) {
          ["clients"]=>
          string(49) "https://apiurl.com/api/companies/93676/clients"
        }
      }
      ["actions"]=>
      array(1) {
        [0]=>
        array(1) {
          ["archive"]=>
          array(2) {
            ["href"]=>
            string(49) "https://apiurl.com/api/companies/93676/archive"
            ["method"]=>
            string(4) "POST"
          }
        }
      }
    }
    [7]=>
    array(6) {
      ["resource"]=>
      string(41) "https://apiurl.com/api/companies/77051"
      ["id"]=>
      int(77051)
      ["name"]=>
      string(11) "siteName"
      ["website"]=>
      string(0) ""
      ["resources"]=>
      array(1) {
        [0]=>
        array(1) {
          ["clients"]=>
          string(49) "https://apiurl.com/api/companies/77051/clients"
        }
      }
      ["actions"]=>
      array(1) {
        [0]=>
        array(1) {
          ["archive"]=>
          array(2) {
            ["href"]=>
            string(49) "https://apiurl.com/api/companies/77051/archive"
            ["method"]=>
            string(4) "POST"
          }
        }
      }
    }
    [8]=>
    array(6) {
      ["resource"]=>
      string(41) "https://apiurl.com/api/companies/77066"
      ["id"]=>
      int(77066)
      ["name"]=>
      string(22) "siteName"
      ["website"]=>
      string(32) "http://siteURL.com"
      ["resources"]=>
      array(1) {
        [0]=>
        array(1) {
          ["clients"]=>
          string(49) "https://apiurl.com/api/companies/77066/clients"
        }
      }
      ["actions"]=>
      array(1) {
        [0]=>
        array(1) {
          ["archive"]=>
          array(2) {
            ["href"]=>
            string(49) "https://apiurl.com/api/companies/77066/archive"
            ["method"]=>
            string(4) "POST"
          }
        }
      }
    }
    [9]=>
    array(6) {
      ["resource"]=>
      string(41) "https://apiurl.com/api/companies/82600"
      ["id"]=>
      int(82600)
      ["name"]=>
      string(9) "siteName"
      ["website"]=>
      string(24) "http://www.siteURL.com/"
      ["resources"]=>
      array(1) {
        [0]=>
        array(1) {
          ["clients"]=>
          string(49) "https://apiurl.com/api/companies/82600/clients"
        }
      }
      ["actions"]=>
      array(1) {
        [0]=>
        array(1) {
          ["archive"]=>
          array(2) {
            ["href"]=>
            string(49) "https://apiurl.com/api/companies/82600/archive"
            ["method"]=>
            string(4) "POST"
          }
        }
      }
    }
    [10]=>
    array(6) {
      ["resource"]=>
      string(41) "https://apiurl.com/api/companies/88116"
      ["id"]=>
      int(88116)
      ["name"]=>
      string(12) "siteName"
      ["website"]=>
      string(26) "http://www.siteURL.com"
      ["resources"]=>
      array(1) {
        [0]=>
        array(1) {
          ["clients"]=>
          string(49) "https://apiurl.com/api/companies/88116/clients"
        }
      }
      ["actions"]=>
      array(1) {
        [0]=>
        array(1) {
          ["archive"]=>
          array(2) {
            ["href"]=>
            string(49) "https://apiurl.com/api/companies/88116/archive"
            ["method"]=>
            string(4) "POST"
          }
        }
      }
    }
    [11]=>
    array(6) {
      ["resource"]=>
      string(41) "https://apiurl.com/api/companies/78383"
      ["id"]=>
      int(78383)
      ["name"]=>
      string(12) "siteName"
      ["website"]=>
      string(22) "http://siteURL.com"
      ["resources"]=>
      array(1) {
        [0]=>
        array(1) {
          ["clients"]=>
          string(49) "https://apiurl.com/api/companies/78383/clients"
        }
      }
      ["actions"]=>
      array(1) {
        [0]=>
        array(1) {
          ["archive"]=>
          array(2) {
            ["href"]=>
            string(49) "https://apiurl.com/api/companies/78383/archive"
            ["method"]=>
            string(4) "POST"
          }
        }
      }
    }
    [12]=>
    array(6) {
      ["resource"]=>
      string(41) "https://apiurl.com/api/companies/82684"
      ["id"]=>
      int(82684)
      ["name"]=>
      string(10) "siteName"
      ["website"]=>
      string(23) "http://www.siteURL.com"
      ["resources"]=>
      array(1) {
        [0]=>
        array(1) {
          ["clients"]=>
          string(49) "https://apiurl.com/api/companies/82684/clients"
        }
      }
      ["actions"]=>
      array(1) {
        [0]=>
        array(1) {
          ["archive"]=>
          array(2) {
            ["href"]=>
            string(49) "https://apiurl.com/api/companies/82684/archive"
            ["method"]=>
            string(4) "POST"
          }
        }
      }
    }
    [13]=>
    array(6) {
      ["resource"]=>
      string(41) "https://apiurl.com/api/companies/85216"
      ["id"]=>
      int(85216)
      ["name"]=>
      string(11) "siteName"
      ["website"]=>
      string(25) "http://www.siteURL.com"
      ["resources"]=>
      array(1) {
        [0]=>
        array(1) {
          ["clients"]=>
          string(49) "https://apiurl.com/api/companies/85216/clients"
        }
      }
      ["actions"]=>
      array(1) {
        [0]=>
        array(1) {
          ["archive"]=>
          array(2) {
            ["href"]=>
            string(49) "https://apiurl.com/api/companies/85216/archive"
            ["method"]=>
            string(4) "POST"
          }
        }
      }
    }
    [14]=>
    array(6) {
      ["resource"]=>
      string(41) "https://apiurl.com/api/companies/85226"
      ["id"]=>
      int(85226)
      ["name"]=>
      string(15) "siteName"
      ["website"]=>
      string(21) "http://siteURL.com"
      ["resources"]=>
      array(1) {
        [0]=>
        array(1) {
          ["clients"]=>
          string(49) "https://apiurl.com/api/companies/85226/clients"
        }
      }
      ["actions"]=>
      array(1) {
        [0]=>
        array(1) {
          ["archive"]=>
          array(2) {
            ["href"]=>
            string(49) "https://apiurl.com/api/companies/85226/archive"
            ["method"]=>
            string(4) "POST"
          }
        }
      }
    }
    [15]=>
    array(6) {
      ["resource"]=>
      string(41) "https://apiurl.com/api/companies/86639"
      ["id"]=>
      int(86639)
      ["name"]=>
      string(8) "siteName"
      ["website"]=>
      string(23) "http://www.siteURL.com"
      ["resources"]=>
      array(1) {
        [0]=>
        array(1) {
          ["clients"]=>
          string(49) "https://apiurl.com/api/companies/86639/clients"
        }
      }
      ["actions"]=>
      array(1) {
        [0]=>
        array(1) {
          ["archive"]=>
          array(2) {
            ["href"]=>
            string(49) "https://apiurl.com/api/companies/86639/archive"
            ["method"]=>
            string(4) "POST"
          }
        }
      }
    }
    [16]=>
    array(6) {
      ["resource"]=>
      string(41) "https://apiurl.com/api/companies/90931"
      ["id"]=>
      int(90931)
      ["name"]=>
      string(7) "siteName"
      ["website"]=>
      string(21) "http://www.siteURL.com"
      ["resources"]=>
      array(1) {
        [0]=>
        array(1) {
          ["clients"]=>
          string(49) "https://apiurl.com/api/companies/90931/clients"
        }
      }
      ["actions"]=>
      array(1) {
        [0]=>
        array(1) {
          ["archive"]=>
          array(2) {
            ["href"]=>
            string(49) "https://apiurl.com/api/companies/90931/archive"
            ["method"]=>
            string(4) "POST"
          }
        }
      }
    }
    [17]=>
    array(6) {
      ["resource"]=>
      string(41) "https://apiurl.com/api/companies/91482"
      ["id"]=>
      int(91482)
      ["name"]=>
      string(9) "siteName"
      ["website"]=>
      string(23) "http://www.siteURL.com"
      ["resources"]=>
      array(1) {
        [0]=>
        array(1) {
          ["clients"]=>
          string(49) "https://apiurl.com/api/companies/91482/clients"
        }
      }
      ["actions"]=>
      array(1) {
        [0]=>
        array(1) {
          ["archive"]=>
          array(2) {
            ["href"]=>
            string(49) "https://apiurl.com/api/companies/91482/archive"
            ["method"]=>
            string(4) "POST"
          }
        }
      }
    }
    [18]=>
    array(6) {
      ["resource"]=>
      string(41) "https://apiurl.com/api/companies/53818"
      ["id"]=>
      int(53818)
      ["name"]=>
      string(17) "siteName"
      ["website"]=>
      string(24) "http://siteURL.com"
      ["resources"]=>
      array(1) {
        [0]=>
        array(1) {
          ["clients"]=>
          string(49) "https://apiurl.com/api/companies/53818/clients"
        }
      }
      ["actions"]=>
      array(1) {
        [0]=>
        array(1) {
          ["archive"]=>
          array(2) {
            ["href"]=>
            string(49) "https://apiurl.com/api/companies/53818/archive"
            ["method"]=>
            string(4) "POST"
          }
        }
      }
    }
    [19]=>
    array(6) {
      ["resource"]=>
      string(41) "https://apiurl.com/api/companies/92924"
      ["id"]=>
      int(92924)
      ["name"]=>
      string(18) "siteName"
      ["website"]=>
      string(31) "http://www.siteURL.com"
      ["resources"]=>
      array(1) {
        [0]=>
        array(1) {
          ["clients"]=>
          string(49) "https://apiurl.com/api/companies/92924/clients"
        }
      }
      ["actions"]=>
      array(1) {
        [0]=>
        array(1) {
          ["archive"]=>
          array(2) {
            ["href"]=>
            string(49) "https://apiurl.com/api/companies/92924/archive"
            ["method"]=>
            string(4) "POST"
          }
        }
      }
    }
  }
}

2 个答案:

答案 0 :(得分:0)

你能试试吗?

<?php foreach($data['companies'] as $company):?>
<li>
<?php echo base_url();?>tasks/display/<?php echo $company['id']; ?>
                    <?php echo $company['name']; ?>
</li>
<?php endforeach; ?>

答案 1 :(得分:0)

对于任何追踪我所面临的错误的人,我是在数组中放入一个数组,所以我有$ data ['companies'] = output,我应该在那里完成$ data =

    <?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Curl extends CI_Controller {


    public function index() {
        $data['companies'] = $this->curl_model->curl_index();


        $data['main_view'] = "companies/display";


        $this->load->view('layouts/main', $data);

}

    } ?>