Solved

PHP Post Field not passing with class and CURL

Posted on 2011-02-18
7
880 Views
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.

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
Comment
Question by:selvol
[X]
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
7 Comments
 
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 &)
0
 
LVL 11

Expert Comment

by:VanHackman
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
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
 
LVL 17

Author Comment

by:selvol
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.
$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
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

by:sjklein42
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.

$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
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 200 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
0
 
LVL 4

Assisted Solution

by:LAMASE
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:
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
 
LVL 17

Author Comment

by:selvol
ID: 34932511
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

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 (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), 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