如何在远程脚本(target_url.php)中检索POSTFEILD var内容以在同一个远程脚本中使用它?
我正在做以下这样的事情。它不返回任何错误。
//Have tried multiple ways to setup the POSTFEILD argument, such as:
$data = array('var'=>'varcontents');
$post_arg = http_build_query($data) . "\n";
//and
$post_arg = 'var =' . urlencode($varcontents);
//create cURL connection
$ch = curl_init('http://www.remotedomain.com/target_url.php');
//set options
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
//set data to be posted
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_arg);
//perform the request
$result = curl_exec($ch);
//show information regarding the request
print_r(curl_getinfo($ch));
echo curl_errno($ch) . '-' . curl_error($ch);
答案 0 :(得分:2)
刚遇到同样的问题,这是我找到的解决方案,我想我会与你分享:
使用
$varcontent = file_get_contents("php://input");
你的target_url.php中的应该可以解决这个问题
答案 1 :(得分:0)
使用
<?php
$post_arg = http_build_query( array('var'=>'varcontents') );
//create cURL connection
$ch = curl_init('http://localhost/target_url.php');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_arg);
$result = curl_exec($ch);
var_dump($result);
作为“客户”和
<?php
var_dump($_POST);
as target_url.php,输出为
string(186) "<pre class='xdebug-var-dump' dir='ltr'>
<b>array</b>
'var' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'varcontents'</font> <i>(length=11)</i>
</pre>"
(提醒我停用xdebug选项;-)),所以在target_url.php中可以通过$ _POST访问参数。
答案 2 :(得分:0)
你的卷曲设置似乎不太对劲。试试这个:
$post_data = http_build_query(array('var'=>'varcontents'));
$url = 'http://www.remotedomain.com/target_url.php';
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$buffer = curl_exec($ch);
curl_close($ch);
在你的target_url.php中,als已经说过:
$varcontent = $_POST['var'];