PHP Post Field not passing with class and CURL

Posted on 2011-02-18
Last Modified: 2012-05-11
With this Curl Example I am using from

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,, mike

// 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)';
if ($this->cookies == TRUE) $this->cookie($cookie);
function cookie($cookie_file) {
if (file_exists($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');
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);
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);
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>";
$cc = new cURL();
// Add URL and Filed


Open in new window

Question by:selvol
LVL 18

Expert Comment

by:Sudaraka Wijesinghe
ID: 34931257
You have to pass the post data in the second parameter as in a form of query string (name=value pairs combine with &)
LVL 11

Expert Comment

ID: 34931374
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

//set POST variables
$url = '';
$fields = array(

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

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data

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

//close connection

Open in new window

LVL 17

Author Comment

ID: 34931585
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.

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

Other times I have just    done

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:, 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

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.

LVL 16

Assisted Solution

sjklein42 earned 150 total points
ID: 34931620
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.


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).
LVL 83

Accepted Solution

Dave Baldwin earned 200 total points
ID: 34931870
"space and comma" are not legal characters in a URL, they should be encoded.  Also

Assisted Solution

LAMASE earned 150 total points
ID: 34932318
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:

A POST request normally sends ALL THE DATA in POST
target script:
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


Open in new window

or even better


Open in new window

LVL 17

Author Comment

ID: 34932511
Most excellent group of fellow experts. I appreciate everyone of you.

Yes,  that was a huge error

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
$query = explode(" ", $_GET['lafi']);
$first = $query[1];
$last  = $query[0];

Open in new window


Open in new window


Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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 dynamically set the form action using jQuery.

829 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