Beispiel #1
0
 /** Helper to get (distant) file content
  *
  * @param string file location
  * @param float http timeout (optional, default to self::timeout 30s)
  * @param integer starting offset (optional, default to null)
  * @param integer content length (optional, default to null)
  *
  * @return string|boolean file content or false if error
  */
 public static function file_GET_contents($file, $timeout = self::timeout, $offset = null, $length = null)
 {
     if (is_file($file) || ini_GET('allow_url_fopen')) {
         $context = !is_file($file) && $timeout ? stream_context_create(array('http' => array('timeout' => $timeout))) : null;
         return !is_null($offset) ? $length ? @file_GET_contents($file, false, $context, $offset, $length) : @file_GET_contents($file, false, $context, $offset) : @file_GET_contents($file, false, $context);
     } elseif (!function_exists('curl_init')) {
         return self::set_error(new Exception('Install CURL or enable "allow_url_fopen"'));
     }
     $handle = curl_init($file);
     if ($timeout) {
         curl_setopt($handle, CURLOPT_TIMEOUT, $timeout);
     }
     if ($offset || $length) {
         curl_setopt($handle, CURLOPT_RANGE, $offset . '-' . ($length ? $offset + $length - 1 : null));
     }
     curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);
     $content = curl_exec($handle);
     $size = curl_GETinfo($handle, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
     curl_close($handle);
     return $offset && $size == -1 || $length && $length != $size ? $length ? substr($content, $offset, $length) : substr($content, $offset) : $content;
 }
Beispiel #2
0
 /**
  * ˽�з���:ִ����������
  *
  * @param string $method :HTTP����ʽ
  * @param string $url :�����URL
  * @param array $params ������IJ���
  * @param array $uploadFile :�ϴ����ļ�(ֻ��POSTʱ����Ч)
  * @param array $referer :����ҳ��
  *
  * @return ���󷵻�:false ��ȷ����:�������
  */
 private function _request($method, $url, $params = array(), $uploadFile = array(), $referer = '')
 {
     //�������GET��ʽ������Ҫ���ӵ�URL����
     if ($method == 'GET') {
         $url = $this->_parseUrl($url, $params);
     }
     //���������URL
     curl_setopt($this->ch, CURLOPT_URL, $url);
     curl_setopt($this->ch, CURLOPT_HTTPHEADER, $this->header);
     //����������ҳ,�����Զ�����
     if ($referer) {
         curl_setopt($this->ch, CURLOPT_REFERER, $referer);
     } else {
         curl_setopt($this->ch, CURLOPT_AUTOREFERER, true);
     }
     curl_setopt($this->ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.3 Safari/537.36");
     $urlInfo = parse_url($url);
     $cookieJar = "/tmp/cookie_{$urlInfo['host']}";
     curl_setopt($this->ch, CURLOPT_COOKIEJAR, $cookieJar);
     //�����POST
     if ($method == 'POST') {
         //����һ�������POST��������Ϊ��application/x-www-form-urlencoded
         curl_setopt($this->ch, CURLOPT_POST, true);
         //����POST�ֶ�ֵ
         $postData = $this->_parsmEncode($params, false);
         //������ϴ��ļ�
         if ($uploadFile) {
             foreach ($uploadFile as $key => $file) {
                 if (is_array($file)) {
                     $n = 0;
                     foreach ($file as $f) {
                         //�ļ������Ǿ��·��
                         $postData[$key . '[' . $n++ . ']'] = '@' . realpath($f);
                     }
                 } else {
                     $postData[$key] = '@' . realpath($file);
                 }
             }
         }
         //pr($postData); die;
         curl_setopt($this->ch, CURLOPT_POSTFIELDS, $postData);
     }
     //�õ��������õ���Ϣ
     $this->info['before'] = curl_GETinfo($this->ch);
     //��ʼִ������
     $result = curl_exec($this->ch);
     //�õ�����ͷ
     $headerSize = curl_GETinfo($this->ch, CURLINFO_HEADER_SIZE);
     $this->info['header'] = substr($result, 0, $headerSize);
     //ȥ������ͷ
     $result = substr($result, $headerSize);
     //�õ����а������������ص���Ϣ
     $this->info['after'] = curl_GETinfo($this->ch);
     //�������ɹ�
     if ($this->getErrCode() == 0) {
         //&& $this->info['after']['http_code'] == 200
         return $result;
     } else {
         return false;
     }
 }