public function download_feed($url = '', $fields = array(), $filename = '', $method = 'GET') { try { $optin = array('CURLOPT_USERAGENT' => 'Unknow Agent', 'CURLOPT_RETURNTRANSFER' => 1, 'CURLOPT_CONNECTTIMEOUT' => 5, 'CURLOPT_FRESH_CONNECT' => TRUE, 'CURLOPT_FORBID_REUSE' => TRUE, 'CURLOPT_AUTOREFERER' => TRUE, 'CURLOPT_FOLLOWLOCATION' => TRUE, 'CURLOPT_MAXREDIRS' => 3, 'CURLOPT_TIMEOUT' => self::API_TIMEOUT); $curl = new dcai\curl($optin); if ($method == 'GET') { $response = $curl->get($url, $fields); } else { if ($method == 'POST') { $response = $curl->post($url, $fields); } else { if ($method == 'PUT') { $response = $curl->put($url, $fields); } else { error_log("Error: unsupport METHOD !"); return FALSE; } } } //testing //error_log(__METHOD__." Finished to download feeds via url=$url and the responses status_code = ".$response->status_code); if (!isset($response->status_code) || $response->status_code == 0) { error_log(__METHOD__ . ' TIMEOUT: api call to ' . $url . ' took more than ' . self::API_TIMEOUT . 's to return'); } else { if ($response->status_code == 200) { if (!empty($response->text)) { if ($this->write_cache_file($filename, $response->text)) { return TRUE; } } else { error_log(__METHOD__ . ' No response data return'); return FALSE; } } else { if ($response->status_code == 401) { error_log(__METHOD__ . ' Unauthorized API request to ' . $url); } else { if ($response->status_code == 404) { error_log(__METHOD__ . ' File not found at: ' . $url); } else { error_log(__METHOD__ . ' Unknow error at ' . $url . " Http code:" . $response->status_code); } } } } return FALSE; } catch (Exception $e) { error_log('General Exception Error: ' . $e->getMessage()); } return FALSE; }
<?php /** * @copyright Dongsheng Cai {@see http://dongsheng.org} * @license http://www.gnu.org/copyleft/gpl.html GNU Public License */ require_once '../curl.class.php'; $curl = new dcai\curl(); $r = $curl->get('http://google.com'); echo $r; var_dump($r);