Solved

curl does not login on wamp

Posted on 2013-06-24
3
590 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

746 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

10 Experts available now in Live!

Get 1:1 Help Now