1
votes

I'm running PHP Version 5.5.12 as part of WAMP.

When I try to execute this code, I'm getting the following error: SSL certificate error: unable to get local issuer certificate This script aims to get content from authentification form passing login and password.

<?php

include('simple_html_dom.php');
ini_set('max_execution_time', 0);
error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('memory_limit', '2G');

class EmailScraping {

    public $url;

    public function __construct($url) {
        if ($this->validUrl($url))
            $this->url = $url;
        else {
            echo 'bad url <br>';
        }
    }

    public function getContent() {

        $username = "****"; 
        $password = "****";

        //login form action url
        $url = 'https://placement.emploiquebec.gouv.qc.ca/mbe/login/portail/auth.asp?provenance=emplr&CL=french';
        $postinfo = "posted&L_OBLG_NOM_UTIL=Votre code d'utilisation&OBLG_NOM_UTIL=" . $username . "&L_OBLG_MOT_PASSE=Votre mot de passe&OBLG_MOT_PASSE=" . $password . "&continuer=CONTINUER&LastLogin&NbEssai=0";

        $cookie_file_path = "./cookies.txt";

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_NOBODY, false);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_ENCODING , "");
        curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
        //set the cookie the site has for certain features, this is optional
        curl_setopt($ch, CURLOPT_COOKIE, "cookiename=0");
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7");
        curl_setopt($ch, CURLOPT_REFERER, $_SERVER['REQUEST_URI']);        
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);    
        curl_setopt($ch, CURLOPT_CAINFO, getcwd() . '\cacert.pem');
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);       
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postinfo);
        $html1 = curl_exec($ch);
        curl_close($ch);

        $ch = curl_init();

        //page with the content I want to grab
        curl_setopt($ch, CURLOPT_URL, "https://placement.emploiquebec.gouv.qc.ca/mbe/ut/suivroffrs/esuivroffrs.asp?CL=french");
        curl_setopt($ch, CURLOPT_POST, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, "");
        $answer = curl_exec($ch);

        if (curl_error($ch)) {
            echo curl_error($ch);
        }

        curl_close($ch);
        return $answer;
    }

    public function validUrl($url) {

        if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i", $url)) {
            return false;
        }
        return true;
    }

}

$scraper = new EmailScraping('https://www.facebook.com');
$result = $scraper->getContent();
$html = "<base href='https://placement.emploiquebec.gouv.qc.ca/mbe/login/portail/auth.asp?provenance=emplr&CL=french' />" . $result;
echo $html;

I'm trying to add this code in php.ini file and restarted WAMP server but still getting the same error.

curl.cainfo="C:/wamp/www/domscraper/cacert.pem"

I really don't know what else to try.

Can anyone advise on what else can I try?

2
Is curl.cainfo="C:/wamp/www/domscraper/cacert.pem" the correct path/file exists? - Antonios Tsimourtos
yes this path/file exists @Antonis Tsimourtos - Dhouha BEN SAID
Maybe try downloading and replacing with this - Antonios Tsimourtos

2 Answers

2
votes

You have curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);

Try setting it to false

Here's a link to some doc - http://php.net/manual/en/function.curl-setopt.php

This should be development only, on prod you should probably resolve any cert issues and would probably need to get certs/keys from the provider.

For error logging add (and check out the docs for additional info)

$verbose = fopen ( 'php://temp', 'w+' );
curl_setopt ( $curlManager, CURLOPT_VERBOSE, true );
curl_setopt ( $curlManager, CURLOPT_STDERR, $verbose );

You might also try removing some of those options if not needed to debug.

-2
votes

This is the bast configuration: change from curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); to curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

I've been trying to connect into API made by Postman and it works.

I use WAMP (PHP7+Apache) on Windows 64bits.