当前位置: 首页>>代码示例>>PHP>>正文


PHP HTTP_Request::getURL方法代码示例

本文整理汇总了PHP中HTTP_Request::getURL方法的典型用法代码示例。如果您正苦于以下问题:PHP HTTP_Request::getURL方法的具体用法?PHP HTTP_Request::getURL怎么用?PHP HTTP_Request::getURL使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在HTTP_Request的用法示例。


在下文中一共展示了HTTP_Request::getURL方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: getHttpResponse

 /**
  * Return array contains the response of the given URL.
  * array[code] => HTTP status code
  * array[headers] => HTTP headers
  * array[headers] => Entity body
  * Throw exception if error.
  *
  * @param  string  $url
  * @param  array   $headers
  * @param  array   $post
  * @return array
  */
 private function getHttpResponse($url, $headers = array(), $post = array())
 {
     $url = str_replace('&', '&', trim($url));
     $req = new HTTP_Request($url, array('allowRedirects' => true, 'maxRedirects' => 5));
     /*
      * @see HTTP_Request_Listener_Extended
      */
     $listener = new HTTP_Request_Listener_Extended();
     $req->attach($listener);
     if (!isset($headers['user-agent'])) {
         $headers['user-agent'] = $this->httpUserAgent;
     }
     foreach ($headers as $key => $value) {
         if (!empty($value)) {
             $req->addHeader($key, $value);
         }
     }
     if (!empty($post)) {
         $req->setMethod('POST');
         foreach ($post as $key => $value) {
             $req->addPostData($key, $value);
         }
     }
     $result = $req->sendRequest();
     $is_error = false;
     if (PEAR::isError($result)) {
         $is_error = true;
         $error_message = $result->getMessage();
         /*
          * $error_message could be empty if the error was raised
          * when fsockopen() returns false in Net_Socket::connect()
          */
         if (empty($error_message)) {
             $error_message = "Failed connecting to the server.";
             /*
              * HTTP_Request raises 'Malformed response' error
              * if request path is empty (e.g. http://www.example.com).
              * This bug still exists in its automatic redirection mechanism
              * in CVS rev. 1.55 (latest as of May 18, 2007).
              */
         } elseif ($error_message == 'Malformed response.') {
             $url = $req->getURL(null);
             if (false !== ($urls = @parse_url($url)) and !isset($urls['path'])) {
                 $req->setURL($url);
                 $result = $req->sendRequest();
                 if (PEAR::isError($result)) {
                     $error_message = $result->getMessage();
                     if (empty($error_message)) {
                         $error_message = "Failed connecting to the server.";
                     }
                 } else {
                     $is_error = false;
                 }
             }
         }
     }
     if ($is_error) {
         throw new Exception($error_message);
     }
     return array('url' => $req->getUrl(null), 'code' => $req->getResponseCode(), 'headers' => $req->getResponseHeader(), 'body' => $req->getResponseBody());
 }
开发者ID:diggin-sandbox,项目名称:mirror-htmlscraping-20090114,代码行数:73,代码来源:HTMLScraping.class.php


注:本文中的HTTP_Request::getURL方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。