我是OOP的新手。我只是在学习,我必须使用它来查找重定向链接的实际/最终URL。
pr
上面的类函数一次又一次地调用相同的函数来查找实际的url。但是我已经在其他文件中调用了这个类
Class ABC {
public function getWebPage($url, $redirectcallback = null){
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1) Gecko/20061024 BonEcho/2.0");
$html = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code == 301 || $http_code == 302) {
list($httpheader) = explode("\r\n\r\n", $html, 2);
$matches = array();
preg_match('/(Location:|URI:)(.*?)\n/', $httpheader, $matches);
$nurl = trim(array_pop($matches));
$url_parsed = parse_url($nurl);
if (isset($url_parsed)) {
if($redirectcallback){ // callback
$redirectcallback($nurl, $url);
}
$html = $this->getWebPage($nurl, $redirectcallback);
}
}
return $html;
}
}
但这不起作用。请建议。
答案 0 :(得分:0)
Class ABC {
public function xyz($url){
$html = $this->xyz($url);
return $html;
}
}
在另一个课程中用
调用它$obj = new ABC;
$url = "Some value";
$someurl = $obj->xyz($url);