我正在尝试使用“PHP Simple HTML DOM Parser”库抓取带有以下代码段的 https 页面:
$html = file_get_html('https://domain.com/');
Run Code Online (Sandbox Code Playgroud)
但它抛出一个错误
SSL operation failed with code 1
Run Code Online (Sandbox Code Playgroud)
我想按照file_get_contents(): SSL operation failed with code 1进行核对验证。此外,解决方案是添加以下内容:
$arrContextOptions=array(
"ssl"=>array(
"verify_peer"=>false,
"verify_peer_name"=>false,
),
);
Run Code Online (Sandbox Code Playgroud)
但我不确定在库的功能中要更改什么:
function file_get_html($url, $use_include_path = false, $context=null, $offset = -1, $maxLen=-1, $lowercase = true, $forceTagsClosed=true, $target_charset = DEFAULT_TARGET_CHARSET, $stripRN=true, $defaultBRText=DEFAULT_BR_TEXT, $defaultSpanText=DEFAULT_SPAN_TEXT)
Run Code Online (Sandbox Code Playgroud)
或者我应该在这里添加什么?:
$dom = new simple_html_dom(null, $lowercase, $forceTagsClosed, $target_charset, $stripRN, $defaultBRText, $defaultSpanText);
$contents = file_get_contents($url, $use_include_path, $context, $offset);
Run Code Online (Sandbox Code Playgroud)
抱歉,我知道这可能是一个简单的解决方案,但在过去的 3 个小时里我一直在挠头试图弄清楚这一点。
小智 5
如果在 file_get_html 函数的左花括号之后粘贴以下内容,它将起作用:
$context = stream_context_create(
array(
'http' => array(
'follow_location' => false
),
'ssl' => array(
"verify_peer"=>false,
"verify_peer_name"=>false,
),
)
);
Run Code Online (Sandbox Code Playgroud)