Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 927
  • Last Modified:

PHP Post Field not passing with class and CURL

With this Curl Example I am using from pHP.com.

I am trying to pass the Post fields in to the url.

Am I just not seeing where to add the post fields data?

See the last line. As that is where I am attempting to add "$postfields".

I am not extremly new to PHP. And have tried several ways to solve this.

Any and all help from my fellow experts is appreciated.

Selvol,

http://example.com/?lafi=williams, mike

<?php
// pcocess multiple name inputs
$query = explode(" ", $_GET['lafi']);
$first = $query[1];
$last  = $query[2];
$postfields = "$last $first";

class cURL {
var $headers;
var $user_agent;
var $compression;
var $cookie_file;
var $proxy;

// Make Sure cookies.txt exists
function cURL($cookies=TRUE,$cookie='cookies.txt',$compression='gzip',$proxy='') {
$this->headers[] = 'Accept: image/gif, image/x-bitmap, image/jpeg, image/pjpeg';
$this->headers[] = 'Connection: Keep-Alive';
$this->headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8';
$this->user_agent = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; Media Center PC 4.0)';
$this->compression=$compression;
$this->proxy=$proxy;
$this->cookies=$cookies;
if ($this->cookies == TRUE) $this->cookie($cookie);
}
function cookie($cookie_file) {
if (file_exists($cookie_file)) {
$this->cookie_file=$cookie_file;
} else {
fopen($cookie_file,'w') or $this->error('The cookie file could not be opened. Make sure this directory has the correct permissions');
$this->cookie_file=$cookie_file;
fclose($this->cookie_file);
}
}
function get($url) {
$process = curl_init($url);

if ($this->cookies == TRUE) curl_setopt($process, CURLOPT_COOKIEFILE, $this->cookie_file);
if ($this->cookies == TRUE) curl_setopt($process, CURLOPT_COOKIEJAR, $this->cookie_file);
curl_setopt($process,CURLOPT_ENCODING , $this->compression);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
if ($this->proxy) curl_setopt($process, CURLOPT_PROXY, $this->proxy);
curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);
$return = curl_exec($process);
curl_close($process);
return $return;
}
function post($url,$data) {
$process = curl_init($url);

if ($this->cookies == TRUE) curl_setopt($process, CURLOPT_COOKIEFILE, $this->cookie_file);
if ($this->cookies == TRUE) curl_setopt($process, CURLOPT_COOKIEJAR, $this->cookie_file);
curl_setopt($process, CURLOPT_ENCODING , $this->compression);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
if ($this->proxy) curl_setopt($process, CURLOPT_PROXY, $this->proxy);
curl_setopt($process, CURLOPT_POSTFIELDS, $data);
curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($process, CURLOPT_POST, 1);
$return = curl_exec($process);
curl_close($process);
echo $return;
return $return;
}
function error($error) {
echo "<center><div style='width:500px;border: 3px solid #FFEEFF; padding: 3px; background-color: #FFDDFF;font-family: verdana; font-size: 10px'><b>cURL Error</b><br>$error</div></center>";
die;
}
}
$cc = new cURL();
// Add URL and Filed
$cc->post('http://.us/search_post.html?action=7CrmeBcr5tY%3D&Victim=$postfields,'');

?>

Open in new window

0
selvol
Asked:
selvol
3 Solutions
 
Sudaraka WijesingheWeb Application ProgrammerCommented:
You have to pass the post data in the second parameter as in a form of query string (name=value pairs combine with &)
0
 
VanHackmanCommented:
For this line:

curl_setopt($process, CURLOPT_POST, 1);


Instead of 1 you should set the number of variables to pass.

For this line:

curl_setopt($process, CURLOPT_POSTFIELDS, $data);


$data should be a single string with all the variables to pass in the "key=value&" format.

I think this example set it clear:

//extract data from the post
extract($_POST);

//set POST variables
$url = 'http://domain.com/get-post.php';
$fields = array(
            'lname'=>urlencode($last_name),
            'fname'=>urlencode($first_name),
            'title'=>urlencode($title),
            'company'=>urlencode($institution),
            'age'=>urlencode($age),
            'email'=>urlencode($email),
            'phone'=>urlencode($phone)
        );

//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);

Open in new window

0
 
selvolAuthor Commented:
Thank you both,


The problem I'm running into is not w/ Curl or how to do curl.

But it is with the last line of the code.
I can not add a dynamic $value to the URL.
$cc->post('http://.us/search_post.html?action=7CrmeBcr5tY%3D&Victim=$postfields,'');

I am self tought in PHP. And do not know exactly what the -> is doing.

Other times I have just    done
'http://.us/search_post.html?action=$value&Victim=Values

But this time it has not worked.

Perhaps   something to do with it being writtes as a class?


What I have is an url like:

http://example.com/form.php?lafi=john, doe
Yes, with the space and comma.

The name can not be split into first and last names before submission.

As it would not work until this problem is solved. .

I am attempting to do the splitting with my code.

$query = explode(" ", $_GET['lafi']);
$first = $query[1];
$last  = $query[2];


But the $fisrt does not end up John in the final line. But shows up as $fisrt literal.

Thank you

VanHackman
sudaraka
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
sjklein42Commented:
Two remarks from the peanut gallery.

On your line 75, why is there a comma after $postfields and before the quote?
I also changed the single quotes to double quotes.  I may be wrong but in Perl at least, variables inside single quotes are not expanded.

$cc->post("http://.us/search_post.html?action=7CrmeBcr5tY%3D&Victim=$postfields");

Open in new window


Also, what is the value of $postfields?  Does it have embedded spaces or special characters?  If so, you need to escape them (%20 for blanks, for example).
0
 
Dave BaldwinFixer of ProblemsCommented:
"space and comma" are not legal characters in a URL, they should be encoded.  http://en.wikipedia.org/wiki/Percent-encoding  Also http://us3.php.net/manual/en/function.urlencode.php
0
 
LAMASECommented:
You are mixing POST and GET. The result depends on how the server application handles it, and if the webserver accepts this kind of things...

As you already know, this is a GET request:
http://www.site.com/file.php?par=val&par=val..

A POST request normally sends ALL THE DATA in POST
target script: http://www.site.com/file.php
post data: par=val&par=val....

Lot of times PHP can do the magic for you, but please spot what the @dave and @sjklein said, in particular the double quotes and the comma

$cc->post("http://.us/search_post.html?action=7CrmeBcr5tY%3D&Victim=$postfields",'');

Open in new window


or even better

$cc->post("http://.us/search_post.html?action=7CrmeBcr5tY%3D&Victim=".urlencode($postfields),'');

Open in new window

0
 
selvolAuthor Commented:
Most excellent group of fellow experts. I appreciate everyone of you.

sjklein42
Yes,  that was a huge error
$postfields,'');

Open in new window

Was two single quotes.  Thank you.

Just to state Mixing POST AND GET seems to work fine the way I used it here.

As for the GET and POST that is ref. with this code
function get($url) 

Open in new window


That was put in the code to allow POST OR GET to be switched on or off with out changing codes.
pyromus at gmail dot com

In the END if used what was my original thought.  

The problem ended up being the server config for the Target.
It parsed " " (spaces) and removed anything after.

So I needed to add "+"
like so
$last+$first.
$query = explode(" ", $_GET['lafi']);
$first = $query[1];
$last  = $query[0];

Open in new window

...................
$cc->post("http://.us/search_post.html?action=7CrmeBcr5tY%3D&Victim=$last+$first");

Open in new window


THANK YOU ALL
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now