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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Part of the Global Positioning System A geocode ( is the major subset of a GPS coordinate (, the other parts being the altitude and t…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

726 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