public static function download($url, $path = "downloads") { if (!self::$ch) { Curl::init(); } $file = fopen($path, "w+"); self::setUrl($url); self::$options[CURLOPT_FILE] = $file; curl_setopt_array(self::$ch, self::$options); return curl_exec(self::$ch); }
public function testHeader() { $c = new Curl(); $c->init(); // testing out of app, so have to initialize manually $result = $c->get('http://echo.jsontest.com/key/value/one/two'); $json = json_decode($result); $this->assertEquals($json->key, 'value'); $headers = $c->getHeaders(); $this->assertContains('HTTP/1.1 200 OK', $headers); }
/** * * @static * @param string $id * @return object|null */ public static function item($id) { self::$_params = 'key=' . self::$_apiKey . '&id=' . $id; Curl::init(); $jsonGet = Curl::get(self::$_apiUrl . '/item?' . self::$_params); Curl::close(); if ($jsonGet) { $jsonData = json_decode($jsonGet); if (isset($jsonData->error)) { showError($jsonData->error, 'API Error', 500); } else { return $jsonData; } } return NULL; }
* Récupère le contenu d'une page ou le contenu d'un fichier de cache * @param string $url URL de la page * @return array Contenu + timestamp de la page $url */ public static function get($url) { $datas = new stdClass(); $fileName = '/' . preg_replace('/https?:\\/\\//', '', $url); curl_setopt(self::$ch, CURLOPT_URL, $url); // On check si le fichier n'existe pas ou s'il a expiré if (!Cache::exists($fileName) || Cache::hasExpired($fileName)) { $datas->url = $url; $datas->timestamp = time(); $datas->content = curl_exec(self::$ch); $datas->info = curl_getinfo(self::$ch); $datas->httpCode = $datas->info['http_code']; $datas->error = curl_error(self::$ch); $datas->errno = curl_errno(self::$ch); Cache::set($fileName, serialize($datas)); $datas->content = json_decode($datas->content); } // Si on a déjà un cache de dispo if (empty((array) $datas)) { $datas = Cache::get($fileName); $datas->content = json_decode($datas->content); } return $datas; } } Curl::init();