Page 1 of 1

exec() not working

Posted: Thu Nov 09, 2006 4:47 pm
by cesarcesar
Hello all,

How to get the $response var to return with a value in it.
____________________________________

http://www.site1.com/test1.php

Code: Select all

$post_array = array(
	 "test1"	=> "a"
	,"test2"	=> "b"
	,"test3"	=> "c"
);

// Convert Array to POST string (key_1=val_1&key_2=val_2...)
reset($post_array);
while (list ($key, $val) = each($post_array)) {
	$data .= $key . "=" . urlencode($val) . "&";
}

exec("/usr/bin/curl -m 120 -d \"$data\" http://www.site2.com/test2.php -L", $response);

// $response returns empty. i need this to have the new $_POST vars in it
print_r($response);
____________________________________

http://www.site2.com/test2.php -

Code: Select all

// my own function. working fine.
custom_insert('test_db');

// set new $_POST vars.
$_POST['x'] = "x";
$_POST['y'] = "y";
$_POST['z'] = "z"; 

// this is not returning properly.
Return $_POST;

Posted: Thu Nov 09, 2006 4:51 pm
by nickvd
Why not use the php curl functions?

Posted: Thu Nov 09, 2006 4:54 pm
by cesarcesar
thought i was via the exec() function.

you have an example. thanks.

Posted: Thu Nov 09, 2006 4:58 pm
by Chris Corbyn
cesarcesar wrote:thought i was via the exec() function.

you have an example. thanks.
So clicked the link he posted I see....

Posted: Thu Nov 09, 2006 4:58 pm
by nickvd
You are using CURL thru the exec function, however php has the curl extension (which should be enabled almost anywhere) which will let you use curl without needing to use exec() (which is not exactly safe and secure). If you read the php page i linked to, you'll find LOTS of examples..

Posted: Thu Nov 09, 2006 9:21 pm
by cesarcesar
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


ok so due to some spanking by senior members in other forums i have been suggested to not use exec() function but just strait CURL. So this is what i have. the final result is a sctring when i thought i was going to get an array(). How do i get an array to show. Current out put is..

array(3) { ["test1"]=>  string(1) "x" ["test2"]=>  string(2) "yy" ["test3"]=>  string(3) "zzz" }

Here are my files - 

curl1.php -

Code: Select all

<?php 

$_POST['test1'] = "1";
$_POST['test2'] = "2";
$_POST['test3'] = "3";

// Convert Array to POST string (key_1=val_1&key_2=val_2...)
reset($_POST);
while (list ($key, $val) = each($_POST)) {
	$data .= $key . "=" . urlencode($val) . "&";
}

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://localhost/clients/curl2.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);

if (curl_errno($ch)) {
   print curl_error($ch);
}

curl_close($ch);

print_r($response);
?>
curl2.php -

Code: Select all

<?php 
unset($_POST);
$_POST['test1'] = "x";
$_POST['test2'] = "yy";
$_POST['test3'] = "zzz";
var_dump($_POST);
?>
When i try to get any value from $responce, i get one character *a*. How do i get $response in the first file to be an array i can use not a string.

Thank you.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Fri Nov 10, 2006 1:05 am
by cesarcesar
nickvd wrote:You are using CURL thru the exec function, however php has the curl extension (which should be enabled almost anywhere) which will let you use curl without needing to use exec() (which is not exactly safe and secure). If you read the php page i linked to, you'll find LOTS of examples..
Can you please provide an example of how it is not safe and secure? Just to make sure Im safe and secure. Im thinking your thinking something php.ini based? Do tell.

Posted: Fri Nov 10, 2006 1:58 am
by cesarcesar
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


OK i got schooled by some 15 year old kid named "Sliver". man i hope i have a job in a few years!!! This is what he had to say about exec() vs. CURL.
[quote] I think you are misinterpreting what CURL is used for. It should be used when you need to execute a form on another site and possibly capture the output which would be HTML, like a normal page. Since you have control of both pages that you are using CURL for, there is no real point.

If you have to use CURL, keep in mind that the CURL response is generally the output of a normal page, not a variable. Therefore, capturing a variable through CURL output would not too easy.[/quote]

I have listed my working code below for any one interested. It basically takes an array (form elements) and passes them to another file on another machine, then passes vars back to the main server. Check this out..

1. Site 1 on Server A sends $_POST vars from to Site 2 on Server A.
2. Site 2 on Server A then sends the same $_POST vars to Site 3 on Server B.
3. Site 3 on Server B processes the $_POST vars.
4. Site 3 on Server B then sends back $responce vars to Site 2 on Server A.
5. Site 2 on Server A then sends $responce vars to Site 1 on Server A.

Put that in your pipe and smoke it. think i will. enjoy, thanks for all the help.

siteA/curl2.php -

Code: Select all

<?php 
$post_array = array(
	 "test1"	=> "x"
	,"test2"	=> "y"
	,"test3"	=> "z"
);

// Convert Array to POST string (key_1=val_1&key_2=val_2...)
reset($post_array);
while (list ($key, $val) = each($post_array)) {
	$data .= $key . "=" . urlencode($val) . "&";
}

exec("/usr/bin/curl -m 120 -d \"$data\" http://www.siteA.com/curl2.php -L", $response);
$response_unserialized = unserialize($response[0]);
print_r(response_unserialized);
?>
siteB/curl2.php -

Code: Select all

<?php 
$response = serialize($_POST);
echo $response;
?>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]