Solved

curl does not login on wamp

Posted on 2013-06-24
3
593 Views
Last Modified: 2013-06-24
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

0
Comment
Question by:rgb192
3 Comments
 
LVL 3

Assisted Solution

by:NewJorg
NewJorg earned 100 total points
ID: 39270622
Hi,

if it is all code, then you are only constructing the object and not doing anything with it.

$craiglist = new craiglist('email', 'pass');
$craiglist->login()

Open in new window


and of course email and password were missing because you would net a constructor like this

public function __construct($email, $pass) {
    $this->email = $email;
    $this->pass = $pass;
  }

Open in new window


then it should do something or tell you anything
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 400 total points
ID: 39271060
cURL errors are not subject to PHP error_reporting() -- they have to be visualized separately.  This code snippet teaches how to detect and show cURL errors.

<?php // RAY_curl_post_example.php
error_reporting(E_ALL);


// DEMONSTRATE HOW TO USE CURL POST TO START AN ASYNCHRONOUS PROCESS


function curl_post($url, $post_array=array(), $timeout=2, $error_report=FALSE)
{
    // PREPARE THE POST STRING
    $post_string = NULL;
    foreach ($post_array as $key => $val)
    {
        $post_string .= $key . '=' . urlencode($val) . '&';
    }
    $post_string = rtrim($post_string, '&');

    // PREPARE THE CURL CALL
    $curl = curl_init();
    curl_setopt( $curl, CURLOPT_URL,            $url         );
    curl_setopt( $curl, CURLOPT_HEADER,         FALSE        );
    curl_setopt( $curl, CURLOPT_POST,           TRUE         );
    curl_setopt( $curl, CURLOPT_POSTFIELDS,     $post_string );
    curl_setopt( $curl, CURLOPT_TIMEOUT,        $timeout     );
    curl_setopt( $curl, CURLOPT_RETURNTRANSFER, TRUE         );

    // EXECUTE THE CURL CALL
    $htm = curl_exec($curl);
    $err = curl_errno($curl);
    $inf = curl_getinfo($curl);

    // ON FAILURE
    if (!$htm)
    {
        // PROCESS ERRORS HERE
        if ($error_report)
        {
            echo "CURL FAIL: $url TIMEOUT=$timeout, CURL_ERRNO=$err";
            echo "<pre>\n";
            var_dump($inf);
            echo "</pre>\n";
        }
        curl_close($curl);
        return FALSE;
    }

    // ON SUCCESS
    curl_close($curl);
    return $htm;
}


// USAGE EXAMPLE CREATES ASSOCIATIVE ARRAY OF KEY=>VALUE PAIRS
$args["name"]  = 'Ray';
$args["email"] = 'Ray.Paseur@Gmail.com';

// ACTIVATE THIS TO SEE THE ARRAY OF ARGS
// var_dump($args);

// SET THE URL
$url = "http://LAPRBass.com/RAY_bounce_post.php";

// CALL CURL TO POST THE DATA
$htm = curl_post($url, $args, 3, TRUE);

// SHOW WHAT CAME BACK, IF ANYTHING
if ($htm)
{
    echo "<pre>";
    echo htmlentities($htm);
}
else
{
    echo "NO RESPONSE YET FROM $url -- MAYBE BECAUSE IT IS RUNNING ASYNCHRONOUSLY";
}

Open in new window

0
 

Author Closing Comment

by:rgb192
ID: 39273638
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
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now