我对这个脚本有一个奇怪的问题。我可以使用 URL:“http://example.com/script.php?payer_email=foo&txn_id=9229fjfua822”直接发布到它。但是尝试从假设发布相同的数据 http://requestmaker.com变量中没有显示任何内容。我在 PHP5 中使用 nginx。
<?php
$panel_url = 'http://example.com:23462/';
$username = $_GET['payer_email'];
$invoice = $_GET['txn_id'];
$trimmedinvoice = substr($invoice, -6);
$password = $trimmedinvoice;
$max_connections = 1;
$reseller = 0;
$bouquet_ids = array(
1,
2,
3 );
$expirationdays = $_GET['custom'];
$expiration = "+$expirationdays day";
$expiredate = strtotime($expiration);
###############################################################################
$post_data = array( 'user_data' => array(
'username' => $username,
'password' => $password,
'max_connections' => $max_connections,
'is_restreamer' => $reseller,
'exp_date' => $expiredate,
'bouquet' => json_encode( $bouquet_ids ) ) );
$opts = array( 'http' => array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => http_build_query( $post_data ) ) );
$context = stream_context_create( $opts );
$api_result = json_decode( file_get_contents( $panel_url . "api.php?action=user&sub=create", false, $context ) );
Echo "<b>Username:</b> <br>";
echo $username;
echo "<br></br>";
echo "<b>Password:<br></b>";
echo $password;
echo "<br></br>";
echo "<b>Expires (in unix time):<br></b>";
echo $expiredate;
?>
测试了一个晚上,发现加上这段代码就可以毫无问题的返回传递过来的数据。所以问题似乎出在脚本上,而不是设置本身。只是想不出我哪里出错了。
print "CONTENT_TYPE: " . $_SERVER['CONTENT_TYPE'] . "<BR />";
$data = file_get_contents('php://input');
print "DATA: <pre>";
var_dump($data);
var_dump($_POST);
print "</pre>";
直接使用 URL 发布的最后一段代码的输出:
CONTENT_TYPE:
DATA:
string(0) ""
array(0) {
}
使用请求者等外部发布者发布的最后一段代码的输出:
CONTENT_TYPE:
text/html<BR />
DATA: <pre>string(35) "payer_email=foo&txn_id=9229fjfua822"
array(0) {
}
最佳答案
POST 变量在 $_POST
中而不是 $_GET
(后者包含附加到 URI 的参数)。
您可以使用包含 POST 和 GET 变量的 $_REQUEST
。
参见 this document了解更多。
关于从网站发布数据时 PHP $get 为空,但不是手动 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41920010/