【发布时间】:2016-02-19 07:02:55
【问题描述】:
我目前正在测试将 POST 数据从网页传递到另一个 URL,并正在尝试研究如何通过 POST 脚本将值从一个页面传递到 URL,然后该 URL 将通过服务器端脚本处理 POST 数据.我正在使用以下 CURL 启动 POST,其中 xxxyyy.com/post_processor.php 是处理脚本的 url。
<?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);
?>
这会按预期发布数据(我已经在 http://requestb.in/12lfg7x1 中对其进行了测试)但是我在处理任何发布的信息时都遇到了问题。
目前,服务器端的处理脚本只包含一个 if 语句,用于查看是否收到任何 post 数据,如果收到则写入数据库:
$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',
)
);
}
这种组合目前不起作用,即我加载了初始脚本,但没有任何数据被 POST,但是没有数据存储在远程数据库中。但是,如果我使用表单将 POST 数据传递给脚本,这确实有效,但是如果我在没有重定向的情况下传递发布数据,则它不起作用。谁能告诉我我在这里做错了什么?
谢谢,
马特
【问题讨论】:
-
你从哪里得到
$entityBody? -
$entityBody = file_get_contents('php://input');