Solved

example to test curl

Posted on 2013-02-04
5
516 Views
Last Modified: 2013-02-09
from the advice of
DaveBaldwin
who is teaching me about curl,

I am looking for a simple example that I can put on my own server to test curl

no mysql, just php hardcoded username, password
form where curl or user can enter username and password

and a cookie is set

afterwards, gettext, 'you have logged in successfully'
0
Comment
Question by:rgb192
  • 2
  • 2
5 Comments
 
LVL 4

Expert Comment

by:tycoot
ID: 38852820
<?php
        // create curl resource
        $ch = curl_init();

        // set url
        curl_setopt($ch, CURLOPT_URL, "example.com");

        //return the transfer as a string
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        // $output contains the output string
        $output = curl_exec($ch);

        // close curl resource to free up system resources
        curl_close($ch);      
?>
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 38852838
Maybe that's what the problem is.  You do not use the form page as the URL in curl, you use the 'action' page of the form to submit your data to.  You are using curl to simulate the action on the form page.  The form page sends the data to the 'action' page so the curl routine should too.
0
 

Author Comment

by:rgb192
ID: 38853143
so what is an basic example of an action page that has a cookie, on successful login
0
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 38853373
Here are three pages for you to test with.  First the 'form' page, next the 'action' page, and finally the 'curl' page.  Make sure and save them under the correct file names or they won't work.

Form-curl-test.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>Form for curl test</title>
</head>
<body>
<h1>Form for curl test</h1>
<form action="php-action-test.php" method="post">
Name: <input type="text" name="name" value="" /><br>
Password: <input type="password" name="password" value="" /><br>
<input type="submit" name="submit" value="submit" />
</form>

</body>
</html>

Open in new window


php-action-test.php
<?php 
// Report all PHP errors (see changelog)
error_reporting(E_ALL);

// verify POST variables
if (!isset($_POST['name']))  $name = ''; else $name = $_POST['name'];
if (!isset($_POST['password']))  $password = ''; else $password = $_POST['password'];

if(($name == "username") && ($password == "password")) {
	$value = "Logged in.";
	setcookie("TestCookie", $value, time()+3600); // this sets the cookie
	$msg = 'You have logged in successfully.';
	}
else {
	$value = "Not logged in.";
	setcookie("TestCookie", $value, time()-3600); // this removes the cookie
	$msg = 'Log in failed.';
	}

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>php-action-test</title>
</head>
<body>
<h1>php-action-test</h1>
<?php echo $msg; ?>

</body>
</html>

Open in new window


php-curl-test.php (you have to change the URL to the 'action' page to work on your server.)
<?php 
// Report all PHP errors (see changelog)
error_reporting(E_ALL);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://10.202.46.40/ee/curltest/php-action-test.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "name=username&password=password");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec ($ch);
curl_close ($ch);

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>php-curl-test</title>
</head>
<body>
<h1>php-curl-test</h1>
<div style="background-color: #ffff33;width:400px;">
<?php echo $output; ?>
</div>
</body>
</html>

Open in new window

0
 

Author Closing Comment

by:rgb192
ID: 38871150
$myFile = "log-php-action-test.html";
$fh = fopen($myFile, 'a');  
$output= "<br>On ".(date("D F d H:i:s",time())).' , name: '. $name . '  , password: ' . $password . ' , message: '.$msg.'<br />';
fwrite($fh, $output);  

php-action-test
I added a log file to see I logged on when I couldnt get it to work

but then I realized that you created output in the $msg


it worked for me, thanks


I have another question about curl testing / adding to see if a cookie is there to go to page3

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28025903.html
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
The viewer will learn how to count occurrences of each item in an array.
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 …

860 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