Jump to content

Please help me with CURL.


fireflywins

Recommended Posts

Hi,

 

I am trying to automated a boring, regular job that I have to do daily. I am trying to automated this job 100% at server end in php by using CURL:

1. Login to a site https://www.tutorsteach.com/login.aspx automatically.

2. then go to this page: https://www.tutorsteach.com/inquiry.aspx.

3. Get the html source code of this page in variable and then scan it for new email ids.

4. Then send them a mail.

 

 

This is my code:

 

<?php

// INIT CURL

$ch = curl_init();

 

// SET URL FOR THE POST FORM LOGIN

curl_setopt($ch, CURLOPT_URL, 'https://www.tutorsteach.com/Default.aspx');

 

// ENABLE HTTP POST

curl_setopt ($ch, CURLOPT_POST, 1);

 

// SET POST PARAMETERS : FORM VALUES FOR EACH FIELD

$EVENTTARGET = "left1$lnkLogin";

$EVENTARGUMENT = "";

$VIEWSTATE = "/wEPDwUKMTcwOTg5NzA3OA9kFgICAQ9kFgQCBQ9kFgICAQ9kFgICAQ8WAh4EVGV4dAWkKTxwPjxzdHJvbmc+V2VsY29tZSB0byBUdXRvcnNUZWFjaC5jb20gLS0gPGZvbnQgY29sb3I9ImJyb3duIj5UaGUgbGFyZ2VzdCB0dXRvciBwcm92aWRlciBpbiB0aGUgcmVnaW9uITwvZm9udD48L3N0cm9uZz48YnIvPjxpPlNpbmNlIDE5OTk8L2k+PC9wPg0KPHA+PGEgaHJlZj0iRmxhc2gvdG91ci5odG0iIG9uY2xpY2s9IndpbmRvdy5vcGVuKCdGbGFzaC90b3VyLmh0bScsJ1JJQycsJ3dpZHRoPTYwMCxoZWlnaHQ9NDM4LHJlc2l6YWJsZT1ubyxzY3JvbGxiYXJzPW5vLHRvb2xiYXI9bm8sbWVudWJhcj1ubyxsb2NhdGlvbj1ubyxzdGF0dXM9bm8sbGVmdD0xMDAsdG9wPTQwLHNjcmVlblg9MTAwLHNjcmVlblk9NDAnKTtyZXR1cm4gZmFsc2U7Ij48aW1nIHNyYz0iaW1hZ2VzL1RvdXIuanBnIiBhbHQ9IlRha2Ugb3VyIFNUVURFTlQgVE9VUiIgYWxpZ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ldyBzdHVkZW50cy4gTmV3IHN0dWRlbnRzIHdpbGwgZmluZCB5b3UuPGJyLz48YSBocmVmPSJmYXEuYXNweCI+PHNtYWxsPkxFQVJOIE1PUkUuLi48L3NtYWxsPjwvYT48L3A+DQpkAgcPZBYCAgEPDxYCHwAFBDIwMTBkZGR/VpP2kfOXZiQ6bnwjYk15PkTukw==";

$EVENTVALIDATION = "/wEWBALly9q5CQKMyrugBwKOhu66DgLz7ZDjD5DRTjhka66u5ECR4filq7BkSxxy";

curl_setopt ($ch, CURLOPT_POSTFIELDS, "left1_tbUser=myusername&left1_tbPassword=mypassword&__EVENTTARGET=".$EVENTTARGET."&__EVENTARGUMENT=".$EVENTARGUMENT."&__VIEWSTATE=".$VIEWSTATE."&__EVENTVALIDATION=".$EVENTVALIDATION);

 

// IMITATE CLASSIC BROWSER'S BEHAVIOUR : HANDLE COOKIES

curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');

 

# Setting CURLOPT_RETURNTRANSFER variable to 1 will force cURL

# not to print out the results of its query.

# Instead, it will return the results as a string return value

# from curl_exec() instead of the usual true/false.

curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

 

// EXECUTE 1st REQUEST (FORM LOGIN)

$store = curl_exec ($ch);

 

//print $store;

 

// SET FILE TO DOWNLOAD

curl_setopt($ch, CURLOPT_URL, 'https://www.tutorsteach.com/inquiry.aspx');

 

// EXECUTE 2nd REQUEST (FILE DOWNLOAD)

$content = curl_exec ($ch);

 

// CLOSE CURL

curl_close ($ch);

 

print $content;

 

?>

 

Now this should display the page that comes after we are logged into the site and visit this page: https://www.tutorsteach.com/inquiry.aspx

 

But it is showing this error:

"Object Moved

This object may be found here."

 

Can someone please help me? I am a novice in CURL.

 

thanks in advance.

 

FireFly

Link to comment
Share on other sites

It sounds like the web server you're connecting to is returning a URL redirect.  You'll need to check the return code of the request (which I'm not sure how to do with cURL) and if it's one of the redirect codes (300, 301, others?) you'll need to hit whichever URL it is telling you it moved to.  Do this until you get a response code of 200 or something in the 4- or 5-hundreds.

Link to comment
Share on other sites

Try:

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);

 

To get the header response use:

preg_match('*HTTP\/1\.1\s([0-9]{3})*is', $result, $status);

echo "$status[1]";

or for the entire header

print_r(curl_getinfo($ch));

 

 

HTH

Teamatomic

Link to comment
Share on other sites

I can't help you much as I don't really know cURL.  Some of my PHP applications do communicate with web servers as if they were a browser though and I use the HttpRequest() object for that.  The documentation is a little buried so I'll link that to you:

http://php.he.net/manual/en/class.httprequest.php

 

More specifically the documentation for the send() method has some examples:

http://php.he.net/manual/en/function.httprequest-send.php

 

In my applications that use HttpRequest, I generally do the following:

1) Create a single HttpRequest object

2) Perform the initial POST to log in to a page

3) Check the response for any cookies from the web server and add them to the HttpRequest object

 

Now if I want to make future requests to the site, I can reuse the HttpRequest object that has the set cookies by doing the following:

1) Change the URL on the HttpRequest object

2) Change the method, GET or POST, as necessary

3) Add any POST data or files to upload

4) Call send

5) Check the response

 

I frequently use this method to write small export applications that upload data to web sites via HTTPS.

 

Following is a small code snippet from my code that you can adjust to set the object up:

<?php
class some_app{
    public function __construct() {
                // snippet...
	$http = new HttpRequest( $this->_url, HTTP_METH_POST );
	$http->addPostFields( array( 'client' => $this->_cfg['www']['client'],
		'key' => $this->_cfg['www']['key'] ) );
	$http->send();
	$cookies = $http->getResponseCookies();
	foreach( $cookies as $cookie ) {
		$http->addCookies( $cookie->cookies );
	}
	$json = json_decode( $http->getResponseBody() );
	$this->_continue = $json->Continue;
	$this->_http = $http;
                // snippet
    }

/**
 * Clears the HTTP request for another send operation.  Call this method from
         * elsewhere in your class to communicate with the web site you've logged into.
 *
 * @param string $url
 * @param array $post
 * @param array $postfiles
 * @param bool $send
 * @return stdClass|bool
 */
private function httpRequest( $url, $post, $postfiles = null, $send = false ) {
	$this->_http->setUrl( $url );
	$this->_http->setPostFields( $post );
	$this->_http->setPostFiles( $postfiles === null ? array() : $postfiles );
	if( $send !== true ) {
		return false;
	}
	$this->_http->send();
	return json_decode( $this->_http->getResponseBody() );
}
}
?>

 

Now I should mention some other things:

 

1) HttpRequest() is just a wrapper for cURL so you can accomplish all of the same with cURL.

 

2) In my case I control both the client (the program simulating the browser) and the server (the website).  Hence I know that I don't have any special handling such as URL redirects, accepting any cookies past the initial login, or worrying about session ID changes.

 

3) The php_http extension is required for HttpRequest() and can be difficult to locate.  I believe it is part of PECL, but I've found DLLs for Windows.

 

I hope that helps you some.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.