【问题标题】:Get data from API key to save into database从 API 密钥获取数据以保存到数据库中
【发布时间】:2017-03-05 18:37:28
【问题描述】:

我正在开发使用 Codeigniter 制作的网站应用程序。我想从 API 密钥中获取数据并将它们保存到数据库中。我有一个基本代码,但它不起作用。我需要哪些文件?我是新手,在完成这项任务时遇到了很多困难。

控制器:

public function ee_cron_job(){

  $decode_data = $this->Campaign_model->get_ee_api();
  $this->db->query($query);
  foreach ($decode_data->result() as $current) {
    $data = array(
          'ee_name' => $current['Name'],
          'ee_clicked' => $current['Clickedcount'],
          'ee_open' => $current['Openedcount'] ,
          'ee_recipient' => $current['Recipientcount'],
          'ee_sent' => $current['Sentcount'],
          'ee_failed' => $current['Failedcount'],
          'ee_unsubscribe' => $current['Unsubscribedcount'],
          'ee_dateedded' => $current['Dateadded'],
          'ee_lastactive' => $current['Lastactivity']
    );
  $this->db->query($query);
  $this->Campaign_model->add($data);
  $this->db->update('ee_campaigns');

 }
} 

型号:

public function get_ee_api() {

  $response = Requests::get("https://api.elasticemail.com/v2/campaign/list?apikey=*", array());
  $this->get_ee_api();
  return json_decode($response->body, true);

}


public function get_data(){

  $query = $this->db->query('SELECT * FROM ee_campaigns');
  foreach ($query->result() as $row)
    {

      echo $row->ee_name . '<br/>' ;
    }
}

首先,错误是: 致命错误:允许的内存大小为 134217728 字节已用尽(尝试分配 5085070 比我添加的 ini_set('memory_limit', '1024M');并发生第二个错误: 致命错误:超过 120 秒的最大执行时间。

我应该怎么做才能让它工作?

【问题讨论】:

    标签: json codeigniter api


    【解决方案1】:

    嗯,你已经让你的模型进入了一个永无止境的循环。

    public function get_ee_api() {
    
      $response = Requests::get("https://api.elasticemail.com/v2/campaign/list?apikey=*", array());
      //$this->get_ee_api(); take this line off. The function is calling it self over and over.
      return json_decode($response->body, true);
    
    }
    

    这应该可以让您摆脱执行时间问题,并希望也能解决内存问题。

    如果您需要遵循 MVC 模式,您需要停止在控制器中执行数据库操作。 获取模型以执行查询并将结果传递回控制器。 循环该结果,我也没有在您的控制器中看到查询变量。

    -- 尝试使用 curl

    public function get_ee_api() {
        $response = $this->get_web_page("https://api.elasticemail.com/v2/campaign/list?apikey=*");
        $resArr = array();
        $resArr = json_decode($response);
        echo "<pre>"; print_r($resArr); echo "</pre>";
    
        //return json_decode($resArr); remove comments if you get proper output
    
        }
    
    
        private function get_web_page($url) {
           $options = array(
                CURLOPT_RETURNTRANSFER => true,   // return web page
                CURLOPT_HEADER         => false,  // don't return headers
                CURLOPT_FOLLOWLOCATION => true,   // follow redirects
                CURLOPT_MAXREDIRS      => 10,     // stop after 10 redirects
                CURLOPT_ENCODING       => "",     // handle compressed
                CURLOPT_USERAGENT      => "test", // name of client
                CURLOPT_AUTOREFERER    => true,   // set referrer on redirect
                CURLOPT_CONNECTTIMEOUT => 120,    // time-out on connect
                CURLOPT_TIMEOUT        => 120,    // time-out on response
            ); 
    
            $ch = curl_init($url);
            curl_setopt_array($ch, $options);
    
            $content  = curl_exec($ch);
    
            curl_close($ch);
    
            return $content;
        }
    

    【讨论】:

    • 我收到此错误:遇到 PHP 错误严重性:通知消息:数组到字符串转换文件名:admin/campaigns.php 视图中的此行:Campaign_model- >get_ee_api(); ?>
    • 确保您引用了数组中的正确元素。看起来数据库正在获取一个数组而不是一个字符串。尝试使用 print_r($response);或行号 php 告诉你
    • 模型中的请求似乎有问题:$response = Requests::get("api.elasticemail.com/v2/campaign/list?apikey=**", array());
    • 你能简单地在浏览器上输入 api url 看看有没有结果吗?
    • 我在模型中添加了 curl 部分来检索数据。你可以试试看你是否得到了所需的响应?
    猜你喜欢
    • 2017-09-24
    • 1970-01-01
    • 1970-01-01
    • 2021-12-11
    • 2018-08-27
    • 2020-09-11
    • 2017-07-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多