Solved

example to test curl

Posted on 2013-02-04
5
512 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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Insert values are dynamic 11 41
AWS EC2 & RDS Instance 5 35
ebay devID, appID, certID, userToken 2 27
How to change the link of an image using md5 in php ? 3 32
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

772 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