Jump to content

exec() not working


cesarcesar

Recommended Posts

Hello all,

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

[url=http://www.site1.com/test1.php]www.site1.com/test1.php[/url]
[CODE]
$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);
[/CODE]
____________________________________

[url=http://www.site2.com/test2.php]www.site2.com/test2.php[/url] -
[CODE]
// 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;
[/CODE]
Link to comment
Share on other sites

You need to print out $_POST instead of returning it.  Return values are only picked up through direct exec() calls.. in this case, you are execing curl which does an HTTP request.  An HTTP request returns values by printing them out.

Start with print_r($_POST) at the end of test2.php, and then adjust the printing format to suit your needs.
Link to comment
Share on other sites

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]
<?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);
?>
[/code]

curl2.php -
[code]
<?php
unset($_POST);
$_POST['test1'] = "x";
$_POST['test2'] = "yy";
$_POST['test3'] = "zzz";
var_dump($_POST);
?>
[/code]

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.
Link to comment
Share on other sites

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.

http://www.siteA.com/curl2.php -
[code]<?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);
?>[/code]

http://www.siteB.com/curl2.php -
[code]<?php
$response = serialize($_POST);
echo $response;
?>[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.