Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


PHP Post Field not passing with class and CURL

Posted on 2011-02-18
Medium Priority
Last Modified: 2012-05-11
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.


http://example.com/?lafi=williams, 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 = 'http://domain.com/get-post.php';
$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:

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

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.

LVL 16

Assisted Solution

sjklein42 earned 600 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 84

Accepted Solution

Dave Baldwin earned 800 total points
ID: 34931870
"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

Assisted Solution

LAMASE earned 600 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: 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


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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

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…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
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 …

719 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