Zend Http客户端:重定向的URL的无效URI提供错误

时间:2013-09-11 08:37:33

标签: php zend-framework redirect zend-http-client

我想获取网址的HTML。基本上我这样做:

$client = new Zend_Http_Client($url);           
$client->setConfig(array('strictredirects' => true, 'timeout'=> 100, 'storeresponse' => true));
$response = $client->request();
$html = $response->getBody();

对于某些重定向的网址,我收到以下错误

  

提供的URI无效

例如,如果您考虑以下网址:

  

http://www.hiexpress.com/redirect?path=hd&brandCode=ex&hotelCode=housl&regionCode=1&localeCode=en&cm_mmc=mdpr--GoogleMaps--ex-_-housl

重定向到另一个网址。当我试图获得lastResponse时,它什么都没给我。我能得到这个URL的html吗? 我尝试了配置选项strictredirects但仍然给出了相同的错误。我能解决这个问题吗?

2 个答案:

答案 0 :(得分:0)

试试这个把它放到你的控制器

  // @$uri your url
  $client = new Zend_Http_Client($uri, array(
    'maxredirects' => 2,
    'timeout'      => 10,
  ));

  // Try to mimic the requesting user's UA
  $client->setHeaders(array(
    'User-Agent' => $_SERVER['HTTP_USER_AGENT'],
    'X-Powered-By' => 'Zend Framework'
  ));

  $response = $client->request();

  $body = $response->getBody();
  $body = trim($body);
// Get DOM
if( class_exists('DOMDocument') ) {
  $dom = new Zend_Dom_Query($body);
} else {
  $dom = null; // Maybe add b/c later
}

$title = null;
if( $dom ) {
  $titleList = $dom->query('title');
  if( count($titleList) > 0 ) {
    $title = trim($titleList->current()->textContent);
    $title = substr($title, 0, 255);
  }
}
$this->view->title = $title;//Title of the page
$description = null;
if( $dom ) {
  $descriptionList = $dom->queryXpath("//meta[@name='description']");
  // Why are they using caps? -_-
  if( count($descriptionList) == 0 ) {
    $descriptionList = $dom->queryXpath("//meta[@name='Description']");
  }
  if( count($descriptionList) > 0 ) {
    $description = trim($descriptionList->current()->getAttribute('content'));
    $description = substr($description, 0, 255);
  }
}
$this->view->description = $description;// Description of the page

答案 1 :(得分:0)

由于某些原因,Zend Http Client无法运行,不得不使用CURL来完成工作。 获取HTML:

$headers = array( "User-Agent:MyAgent/1.0\r\n");
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION,  TRUE);
curl_setopt($curl, CURLOPT_MAXREDIRS, 50);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($curl, CURLOPT_TIMEOUT, 20);
$html = curl_exec($curl);
curl_close($curl);
echo $html;

获取有效网址/重定向网址:

$headers = array( "User-Agent:MyAgent/1.0\r\n");
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION,  TRUE);
curl_setopt($curl, CURLOPT_MAXREDIRS, 50);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($curl, CURLOPT_TIMEOUT, 20);
$content = curl_exec($curl);
$redirectedUrl = curl_getinfo($curl, CURLINFO_EFFECTIVE_URL);
curl_close($curl);
echo $redirectedUrl;