Link to home
Start Free TrialLog in
Avatar of rgb192
rgb192Flag for United States of America

asked on

curl does not login on wamp

curl is installed on wamp and other curl scripts work
this code logs on using remote server, ide, but not my wamp
ini_set("display_errors", true);
displays no reason why

class craiglist
{
  public $email;
  public $pass;
  public $cookie;
  public $url;
  public $curl_info;
  public $doPost;
  public $postData;
  public $response;
  public $last_url;
  public $referer;
  public $userAgent;
  public $logged_in;

  //function to login to CL
  public function login()
  {
    //if the cookie file already exists check whether the client is already logged in or not
    if(file_exists($this->cookie)){
      $this->url="https://accounts.craigslist.org/login";
      $this->doPost=0;
      $this->get_source();
      
      //if the client is logged in return true
      if(preg_match("/<a href=\"\/logout\">log out<\/a>/siU",$this->response)){
        $this->log("Login successful");
        return true;
        }
    }

    //else login again

    file_put_contents($this->cookie,"");
    $this->referer="https://accounts.craigslist.org/login";
    $this->postData="step=confirmation&rt=&rp=&inputEmailHandle={$this->email}&inputPassword={$this->pass}";
    $this->doPost=1;
    
    $this->get_source();

    //if logout link not found then show login failed error else show success message
    if(preg_match("/<a href=\"\/logout\">log out<\/a>/siU",$this->response)){
      $this->log("Login successful");
      return true;
      }
    $this->log("Login failed");
    return false;
  }

  //function to send php curl requests
  public function get_source()
  {
    $ch=curl_init($this->url);
    curl_setopt($ch,CURLOPT_HEADER,0);  
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);  
    curl_setopt($ch,CURLOPT_BINARYTRANSFER,0);  
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
    curl_setopt($ch,CURLOPT_REFERER,$this->referer);  
    curl_setopt($ch,CURLOPT_TIMEOUT,30);
    curl_setopt($ch,CURLOPT_USERAGENT,$this->userAgent);  
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);
    curl_setopt($ch,CURLOPT_COOKIE,1);
    curl_setopt($ch,CURLOPT_COOKIEFILE,$this->cookie);  
    curl_setopt($ch,CURLOPT_COOKIEJAR,$this->cookie);
    //if post required send post
    if($this->doPost){
      curl_setopt($ch,CURLOPT_POST,1);
      curl_setopt($ch,CURLOPT_POSTFIELDS,$this->postData);
    }
    $data=curl_exec($ch);
    $data=preg_replace('/\s{2,}/',' ',$data);
    $this->response=$data;
    $info=curl_getinfo($ch);
    $this->curl_info=$info;
    $this->last_url=$info['url'];
  }

}

Open in new window


<?php
ini_set("display_errors", true);
        require_once('core.class.php');
      new craiglist('email','pass');

Open in new window

SOLUTION
Avatar of NewJorg
NewJorg

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rgb192

ASKER

I liked ray's example better because it showed me about curl errors not being phperrors

both my ide and wamp worked on curl example