1

I'm trying to scrape a https page with the following snippet using the "PHP Simple HTML DOM Parser" library:

$html = file_get_html('https://domain.com/');

But it's throwing an error

SSL operation failed with code 1

I want to nuke verification as per file_get_contents(): SSL operation failed with code 1. And more , the solution was to add this:

    $arrContextOptions=array(
    "ssl"=>array(
    "verify_peer"=>false,
    "verify_peer_name"=>false,
    ),
);  

But I'm not sure what to change within the library's function:

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)

or is it something I should be adding here?:

$dom = new simple_html_dom(null, $lowercase, $forceTagsClosed, $target_charset, $stripRN, $defaultBRText, $defaultSpanText);

$contents = file_get_contents($url, $use_include_path, $context, $offset);

Sorry I know it's probably a simple solution but I've been scratching my head for the past 3 hours trying to figure this out.

Community
  • 1
  • 1
Robert Sinclair
  • 4,550
  • 2
  • 44
  • 46

1 Answers1

8

If you paste the following right after the open curly brace for the file_get_html function, it will work:

$context = stream_context_create(
    array(
        'http' => array(
            'follow_location' => false
        ),
        'ssl' => array(
            "verify_peer"=>false,
            "verify_peer_name"=>false,
        ),
    )
);

enter image description here

Catarina Ferreira
  • 1,824
  • 5
  • 17
  • 26
Erik Leisch
  • 96
  • 1
  • 3
  • 2
    Thanks for answering Erik. It's generally best if you can put all the relevant code into your answer, rather than link to an image of it. – Alex Young Dec 02 '16 at 17:18
  • Thanks a lot, that works for me: `$context=stream_context_create(array('ssl'=>array('verify_peer'=>false,'verify_peer_name'=>false)));` – adilbo Jul 09 '18 at 08:39
  • Just as a late notice: The part with the follow_location can cause your code to fail since you may receive a 3** code with a status page. So if you run into blank pages, you may set this to true or don't include it all since true should be default. – maio290 Jan 14 '19 at 13:58
  • worked perfectly, thank you very much for your time! [+1] – kapitan Apr 11 '20 at 05:13