php cURL没有收到数据

I have a page a.php that uses cURL to post data to b.php page. However, when the page header to b.php, I didn't receive any POST data.

Here is my code:

$data=array();
$data['firstname']='A';
$data['lastname']='B';

$post_str='';
foreach($data as $key=>$value){
    $post_str.=$key.'='.urlencode($value).'&';  
}

$post_str=substr($post_str, 0, -1);//Remove last & from loop

$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://localhost/b.php');
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_str);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

$response=curl_exec($ch);

curl_close($ch);
header('location:b.php');

b.php page

echo $_POST['firstname'];

That's because this call:

header('location:b.php');

relocates to b.php without any $_POST data.

You have two php scripts, but they aren't really connected as far as the browser is concerned. The web, after all, was intended to be stateless. Your a.php may very well be successfully posting to b.php.

But the header('location:b.php'); line calls b.php without any parameters.

To make a.php a little more generic, I changed
curl_setopt($ch, CURLOPT_URL, 'http://localhost/b.php');
to:
curl_setopt($ch, CURLOPT_URL, $_SERVER['SERVER_NAME'].'/b.php');

b.php needs to record and then display results:

<?
// if called without POST data, dump the last POST,
// otherwise record the POST in a file

$filename = 'temp_data.txt';
if (count($_POST) == 0) {
    if (file_exists($filename)) {
        // dump recorded results
        echo "<pre>
";
        readfile($filename);
        echo "</pre>
";
    } else {
        echo 'No file available.';
    }

} else {
// record results:
    $fp = fopen($filename, 'w');
    fwrite($fp, print_r($_POST, true));
    fclose($fp);
}

So open a.php, THEN open b.php in your browser.