我正在使用此代码使用cURL获取数据
$url='http://example.com/'; //URL to get content from..
print_r(get_data($url)); //Dumps the content
/* Gets the data from a URL */
function get_data($url)
{
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
Run Code Online (Sandbox Code Playgroud)
但是,此代码返回具有相对URL的数据.如何通过绝对URL获取此相对URL和打印?可能与preg_replace ..但如何?
看看HTML base标签.如果您想让浏览器执行所有相对于绝对的转换,您会发现它很有用:
$data = get_data($url);
// Note: ideally you should use DOM manipulation to inject the <base>
// tag inside the <head> section
$data = str_replace("<head>", "<head><base href=\"$url\">", $data);
echo $data;
Run Code Online (Sandbox Code Playgroud)