/** * internal function that I use to make all the requests to flickr * * @param string $method The Flickr Method that is being requested * @param array $params An array of the various required and optional fields needed to make the mthod request * * @return array The xml turned into an array * @access public */ function makeRequest($method, $params) { $this->_clearErrors(); $useCURL = in_array('curl', get_loaded_extensions()); $params['method'] = $method; $params['api_key'] = $this->_api_key; $args = array(); foreach ($params as $k => $v) { array_push($args, urlencode($k) . '=' . urlencode($v)); } $query_str = implode('&', $args); $request = $this->_flickr_api_url . '?' . $query_str; // full url to request $hit_flickr = true; // whether or not to make a request to flickr $request_hash = md5($request); if ($this->_cache_enabled) { if ($this->_cache_type == 'db') { $now = time(); $rows = $this->_cache->findMany("WHERE request = '" . $request_hash . "' AND date_expire > {$now}"); // if any rows found, then use cached response if (count($rows) > 0) { $xml = $rows[0]->response; $hit_flickr = $xml == '' ? true : false; } } else { $now = time(); $file = $this->_cache_dir . md5($request) . '.cache'; if (file_exists($file)) { $xml = file_get_contents($file); $hit_flickr = $xml == '' ? true : false; } } } // only hit flickr if cached request not found above if ($hit_flickr) { // whether or not to use curl for request if ($useCURL) { $c = new curl($request); $c->setopt(CURLOPT_FOLLOWLOCATION, true); $xml = $c->exec(); $error = $c->hasError(); if ($error) { $this->_error_msg = $error; return false; } $c->close(); } else { // curl not available so use fsockopen $url_parsed = parse_url($request); $host = $url_parsed["host"]; $port = $url_parsed['port'] == 0 ? 80 : $url_parsed['port']; $path = $url_parsed["path"] . ($url_parsed['query'] != '' ? $path .= "?{$url_parsed[query]}" : ''); $headers = "GET {$path} HTTP/1.0\r\n"; $headers .= "Host: {$host}\r\n\r\n"; $fp = fsockopen($host, $port, $errno, $errstr, 30); if (!$fp) { $this->_error_msg = $errstr; $this->_error_code = $errno; return false; } else { fwrite($fp, $headers); while (!feof($fp)) { $xml .= fgets($fp, 1024); } fclose($fp); /* this seems stupid, but it removes the headers from the response; if you know a better way let me know */ $xml_start = strpos($xml, '<?xml'); $xml = substr($xml, $xml_start, strlen($xml)); } } if ($this->_cache_enabled) { // store the cached request if ($this->_cache_type == 'db') { $this->_cache->request = $request_hash; $this->_cache->response = $xml; $this->_cache->date_expire = strtotime("+ {$this->_cache_expire} seconds", time()); $this->_cache->save(); } else { $file = $this->_cache_dir . $request_hash . '.cache'; $fp = fopen($file, "w"); $result = fwrite($fp, $xml); fclose($fp); } } } if ($this->_debug) { echo '<h2>XML Response</h2>'; echo '<pre class="xml">'; echo htmlspecialchars($xml); echo '</pre>'; } $xml_parser = xml_parser_create(); xml_parse_into_struct($xml_parser, $xml, $data); xml_parser_free($xml_parser); return $data; }
//called dummy_log $darray = array('file_size' => $curl->file_size, 'file_name' => $file_name . '.' . $ext, 'time_started' => time(), 'byte_size' => 0); $do = fopen($dummy_file, 'w+'); fwrite($do, json_encode($darray)); fclose($do); } //Opening video file $temp_fo = fopen($svfile, 'w+'); $curl->setopt(CURLOPT_FILE, $temp_fo); // Set up the callback if (phpversion() >= '5.3.0') { $curl->setopt(CURLOPT_NOPROGRESS, false); $curl->setopt(CURLOPT_PROGRESSFUNCTION, 'callback'); } $curl->exec(); if ($theError = $curl->hasError()) { $array['error'] = $theError; echo json_encode($array); } //Finish Writing File fclose($temp_fo); sleep(10); $details = file_get_contents($log_file); $details = json_decode($details, true); $Upload->add_conversion_queue($details['file_name']); if (file_exists($log_file)) { unlink($log_file); } if (file_exists($dummy_file)) { unlink($dummy_file); }
private function curl_post($URL = NULL, $POST_DATA = NULL) { if ($URL == NULL || $POST_DATA == NULL) { trigger_error("curl_post() ERROR: URL or POST_DATA has not been setted.", E_USER_ERROR); } $URL = new curl($URL); $URL->setopt(CURLOPT_FOLLOWLOCATION, TRUE); $URL->setopt(CURLOPT_SSL_VERIFYPEER, FALSE); $URL->setopt(CURLOPT_SSL_VERIFYHOST, FALSE); $URL->setopt(CURLOPT_POST, TRUE); $URL->setopt(CURLOPT_POSTFIELDS, $POST_DATA); $URL->setopt(CURLOPT_USERAGENT, "User-Agent: IIC2.0/PC 2.3.0230"); $curl_result = $URL->exec(); if ($theError = $URL->hasError()) { echo $theError; } $URL->close(); return $curl_result; }
<?php include_once "class.curl.php"; // // Create a new instance of the curl class and point it // at the page to be fetched. // $c = new curl("http://www.csworks.com/resume/cv.shtml"); // // By default, curl doesn't follow redirections and this // page may or may not be available via redirection. // $c->setopt(CURLOPT_FOLLOWLOCATION, true); // // By default, the curl class expects to return data to // the caller. // echo $c->exec(); // // Check to see if there was an error and, if so, print // the associated error message. // if ($theError = $c->hasError()) { echo $theError; } // // Done with the cURL, so get rid of the cURL related resources. // $c->close();