2017-03-05 183 views
0

我正在使用Codeigniter製作網站應用程序。我想從API密鑰中獲取數據並將它們保存到數據庫中。我有一個基本代碼,但它不工作。我需要哪些文件?我是這個新手,在完成這項任務時遇到很多困難。從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');並且發生了第二個錯誤: 致命錯誤:Maximum ex超過120秒的行程時間。

我該怎麼做才能使它工作?

回答

0

那麼你已經把你的模型放在永無止境的循環中。

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模式,則需要停止在控制器中執行數據庫操作。 獲取模型執行查詢並將結果傳遞迴控制器。 循環結果,我沒有看到控制器中的查詢變量。

- 與捲曲嘗試

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; 
    } 
+0

我得到這個錯誤:遇到一個PHP錯誤 嚴重性:注意 消息:數組字符串轉換 文件名:管理/此行campaigns.php在視圖中:<?php echo $ this-> Campaign_model-> get_ee_api(); ?> – Dasa

+0

確保你指的是數組中的正確元素。它看起來像DB正在獲取一個數組而不是一個字符串。嘗試使用print_r($ response);或在行號php告訴你 –

+0

它看起來像是模型中的機智請求是錯誤的:$ response = Requests :: get(「https://api.elasticemail.com/v2/campaign/list?apikey=** 「,array()); – Dasa

相關問題