处理POST数据服务器端

I'm currently testing passing POST data from a web page to another URL and am trying to work out how to pass values from one page via a POST script to a URL which will then process the POST data via a server side script. I'm using the following CURL to initiate the POST where xxxyyy.com/post_processor.php is the url to the processing script.

<?php

//set POST variables
$url = 'http://xxxyyy.com/post_processor.php';
$fields = array(
    'lname' => 'smith',
    'fname' => 'peter',
);

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);

?>

This posts data as expected (I've tested it in http://requestb.in/12lfg7x1) however I'm having trouble processing any of the POSTed information.

At the moment, the processing script on the server side simply contains an if statement to see whether any post data has been received and if so writes to a database:

$entityBody = file_get_contents('php://input');  

if (!empty($entityBody))
    {
        // handle post data

    global $wpdb;

            $table_name = $wpdb->prefix . 'post_data_1';
            $wpdb->insert( 
            $table_name, 
            array( 
                'time' => current_time( 'mysql' ), 
                'post_data_recieved' => 'ok',
            )
        );

}

This combination currently doesn't work, i.e. I load the initial script and nothing happens data is POSTed however no data is stored in the remote database. This does however work if I use a form to pass the POST data to the script however it doesn't work if I pass the post data without the redirect. Can anyone tell me what I'm doing wrong here?

Thanks,

Matt

This is essentially the same as what was posted originally and works in that the return value is successfully printed to screen.

The fact that the db is not being updated makes me think that perhaps the db connection is not available within the post_processor.php script so does post_processor.php have all the necessary included files to ensure that $wpdb is available?

<?php
    $url = 'http://xxxyyy.com/post_processor.php';
    $url = 'https://localhost/target.php';


    $fields = array(
        'lname'     => 'smith',
        'fname'     => 'peter'
    );
    /* prepare the data for transmission as a urlencoded string */
    $data=http_build_query( $fields );



    $ch = curl_init();

    if( parse_url( $url,PHP_URL_SCHEME )=='https' ){
        /* my server runs https so I need these lines*/
        curl_setopt( $ch, CURLOPT_CAINFO, realpath('c:/wwwroot/cacert.pem') );
        curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 2 );
        curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
    }

    curl_setopt( $ch, CURLOPT_URL, $url );
    curl_setopt( $ch, CURLOPT_POST, true );
    curl_setopt( $ch, CURLOPT_POSTFIELDS, $data );
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
    curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Content-Length: '.strlen( $data ) ) );

    $result = curl_exec( $ch );
    $info = curl_getinfo( $ch );
    curl_close( $ch );

    print_r( $result );
?>



<?php

    $data=file_get_contents('php://input');
    print_r( $data );

    /* what would this show ? Object hopefully I guess */
    echo '$wpdb = '.gettype( $wpdb );
?>

outputs:
lname=smith&fname=peter