SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Addict
    Join Date
    Jun 2005
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    exec() not working

    Hello all,

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

    www.site1.com/test1.php
    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);
    ____________________________________

    www.site2.com/test2.php -
    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;

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    edited---


    i dont really understand your command line call to curl, but i think i see the problem conceptually.

    instead of
    return $_POST;
    try
    echo serialize($_POST);

    then
    print_r($response);
    and use unserialize()

    does your php install not have curl built in?
    www.php.net/curl

  3. #3
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Two general exec() tips:

    1. Echo your command before passing it to exec(). Try to run the same from the command line.

    2. Always append "2>&1" to the command to capture error messages along with regular output.

  4. #4
    SitePoint Wizard bronze trophy devbanana's Avatar
    Join Date
    Apr 2006
    Location
    Pennsylvania
    Posts
    1,736
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Why not just use the CURL extension?

  5. #5
    SitePoint Addict
    Join Date
    Jun 2005
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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);
    ?>
    curl2.php -
    Code:
    <?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.

  6. #6
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    did you see my post?

  7. #7
    SitePoint Addict
    Join Date
    Jun 2005
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes i seen the reply. thanks for your suggestion, i have implemented it with some other code thanks.

  8. #8
    SitePoint Addict
    Join Date
    Jun 2005
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.
    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.
    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);
    ?>
    http://www.siteB.com/curl2.php -
    Code:
    <?php 
    $response = serialize($_POST);
    echo $response;
    ?>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •