Php Zend Http客户端:为重定向的URL提供的URI无效错误
我想得到一个URL的html。基本上我是这样做的:Php Zend Http客户端:为重定向的URL提供的URI无效错误,php,zend-framework,redirect,zend-http-client,Php,Zend Framework,Redirect,Zend Http Client,我想得到一个URL的html。基本上我是这样做的: $client = new Zend_Http_Client($url); $client->setConfig(array('strictredirects' => true, 'timeout'=> 100, 'storeresponse' => true)); $response = $client->request(); $html = $response->getBody();
$client = new Zend_Http_Client($url);
$client->setConfig(array('strictredirects' => true, 'timeout'=> 100, 'storeresponse' => true));
$response = $client->request();
$html = $response->getBody();
对于一些重定向的URL,我得到以下错误
提供的URI无效
例如,如果您考虑以下URL:
它重定向到另一个URL。当我试图得到最后的回应时,它什么也没有给我。如何获取此URL的html??我尝试了配置选项strictredirects,但仍然出现了相同的错误。我如何解决这个问题???试试这个,把它放到你的控制器里
// @$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
由于某些原因,Zend Http客户端无法工作,必须使用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;
要获取有效的Url/重定向的Url:
$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;
您能否尝试在配置中使用
strictredirects=false