0

I want to scrape some data in the website but before that I must login first. I used PHP Curl to remotely access Laravel website. I don't know if the execution of logging in was successful because it direct me to page that says "The page has expired due to inactivity. Please refresh and try again.". Sorry for I am really new in PHP curl :(.

Note: I am using NATIVE PHP, I found some ways but only applicable for laravel. :(

This is my code:

**This is my client.php**
public function login()
{
    // TODO: Implement login() method.
    $this->curl->setPost(array("email" => Client::USER_HTTP, 
    "password" => 
    Client::PASS_HTTP));
    $this->curl->setHTTP(Client::USER_HTTP, Client::PASS_HTTP);
    $response = $this->curl->execute("http://nabepero.xyz/login/");
    $this->curl->closeSession();

    return $response;
}

**This is my index.php**
include 'vendors/simple_html_dom.php';
include 'vendors/Curl.php';
include 'src/Client.php';

$client = new Client();
$response = $client->login();
echo $response;

**This is my curl.php**
<?php
/*
* Curl Class
*/
class Curl {
    /**
     * @var $ch
     */
    private $ch;

    /**
     * @const
     */
    const COOKIE_PATH = '/cookie.txt';

    /**
     * The contents of the "User-Agent: " header to be used in a HTTP 
       request.
     * @var string
     */
    private $userAgent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en- 
    US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6';

    /**
     * Curl constructor.
     */
    function __construct()
    {
        if($this->ch == null)
        {
            $this->ch = curl_init();
        }
    }

    /**
     * @param array $params
     * @return $this
     */
    public function setPost($params = array())
    {
        curl_setopt($this->ch, CURLOPT_POST, true);
        curl_setopt($this->ch, CURLOPT_POSTFIELDS, $params);

        return $this;
    }

    /**
     * @param string $username
     * @param string $password
     * @return $this
     */
    public function setHTTP($username = '', $password = '')
    {
        curl_setopt($this->ch,CURLOPT_HTTPAUTH, 
    constant('CURLAUTH_ANY'));
        curl_setopt($this->ch,CURLOPT_USERPWD, $username . ':' . 
    $password);

        return $this;
    }

    /**
     * Execute the request & return the results
     * @param $url
     * @return mixed
     * @throws Exception
     */
    public function execute($url)
    {
        curl_setopt($this->ch, CURLOPT_USERAGENT, $this->userAgent);
        curl_setopt($this->ch, CURLOPT_URL, $url);
        curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION,TRUE);
        curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($this->ch, CURLOPT_VERBOSE, true);
        curl_setopt($this->ch, CURLOPT_TIMEOUT, 60); 
        curl_setopt($this->ch, CURLOPT_REFERER, $url); 

        $cookieFile = dirname(__FILE__).self::COOKIE_PATH;
        curl_setopt ($this->ch, CURLOPT_COOKIESESSION, true);
        curl_setopt ($this->ch, CURLOPT_COOKIEJAR, $cookieFile);
        curl_setopt ($this->ch, CURLOPT_COOKIEFILE, $cookieFile);

        $response = curl_exec ($this->ch);
        if ($response === FALSE)
        {
            curl_close($this->ch);
            throw new \Exception("Opps, An error has occurred while 
    trying scrape data!",500);
        }
        return $response;
    }

    /**
     * close a session
     */
    public function closeSession()
    {
        curl_close($this->ch);
    }
}
?>
jEIZ ART
  • 9
  • 6

0 Answers0