使用PHP中的curl从URL获取HTML [英] Get HTML from URL using curl in PHP
问题描述
我正在尝试使用curl从URL获取HTML源。
I am trying to get HTML source from URL using curl.
下面的代码在localhost中可以很好地工作,但是当移至服务器时它不会返回任何内容:
The below code works perfectly in localhost but it does not return anything when moved to server:
function get_html_from_url($url) {
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
CURLOPT_FOLLOWLOCATION => false,
CURLOPT_ENCODING => "",
CURLOPT_USERAGENT => "User-agent: Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420.1 (KHTML, like Gecko) Version/3.0 Mobile/3B48b Safari/419.3",
CURLOPT_AUTOREFERER => true,
CURLOPT_CONNECTTIMEOUT => 30,
CURLOPT_HTTPHEADER => array(
"Host: host.com",
"Upgrade-Insecure-Requests: 1",
"User-Agent: Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Mobile Safari/537.36",
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
"Accept-Encoding: gzip, deflate",
"Accept-Language: en-US,en;q=0.9",
"Cookie: JSESSIONID=SESSSIONID",
"Connection: close"
),
CURLOPT_TIMEOUT => 30,
CURLOPT_MAXREDIRS => 10,
CURLOPT_SSL_VERIFYPEER => false,
);
$ch = curl_init( $url );
curl_setopt_array( $ch, $options );
$content = curl_exec( $ch );
$err = curl_errno( $ch );
$errmsg = curl_error( $ch );
$header = curl_getinfo( $ch );
curl_close( $ch );
$header['errno'] = $err;
$header['errmsg'] = $errmsg;
$header['content'] = $content;
return $header;
}
我在服务器上收到超时错误,甚至尝试增加超时,但是没有
I get timeout error on server and I even tried to increase the timeout but no luck!
谢谢。
推荐答案
如果我正确理解了您的要求,以下脚本将带您到达那里。您可以使用 htmlspecialchars()来获得所需的功能
If I correctly understood your requirement, the following script should get you there. There is a function you can make use of htmlspecialchars() to get the desired output.
<?php
function get_content($url) {
$options = array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_USERAGENT => "Mozilla/5.0",
);
$ch = curl_init( $url );
curl_setopt_array( $ch, $options );
$htmlContent = curl_exec( $ch );
curl_close( $ch );
return $htmlContent;
}
$link = "https://stackoverflow.com/questions/52477020/get-html-from-a-url-using-curl-in-php";
$response = get_content($link);
echo htmlspecialchars($response);
?>
我在脚本中使用的链接只是一个占位符。随时用您想要的那个替换它。
The link I've used within the script is just a placeholder. Feel free to replace that with the one you are after.
这篇关于使用PHP中的curl从URL获取HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!