Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

curl does not login on wamp

Posted on 2013-06-24
3
Medium Priority
?
612 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 400 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 111

Accepted Solution

by:
Ray Paseur earned 1600 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

636 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