Solved

curl does not login on wamp

Posted on 2013-06-24
3
602 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 110

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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

724 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