我使用“curl”将一些变量从我的网站发送到远程URL:
$url = "a remote url";
$fields =array( variables );
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
$output = curl_exec ($ch);
if (!curl_exec($ch)) {
echo 'An error has occurred: ' . curl_error($ch);
}
else {
echo 'everything was successful';
}
curl_close ($ch);
echo $output;
我得到“一切都很成功”,但我不知道如何从远程网址显示收到的变量。
有人可以告诉我如何显示“$ _POST”中的变量吗? 我想检查变量是否正确发送到远程URL。
答案 0 :(得分:0)
简单使用:
print_r( $_POST );
或者在“发送”脚本中将您的字段显示为数组:
print_r( $fields );
让我们假设你的CURL回复是纯文本的:
variable1=value2
variable2=value3
您可以通过以下方式查看您的回复:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
$output = curl_exec ($ch);
if (!curl_exec($ch)) {
echo 'An error has occurred: ' . curl_error($ch);
}
else {
$rows = explode("\n", $output);
for( $i=0;$i<count($rows);$i++ )
{
$cols = explode('=', trim($rows[$i]) );
print_r($cols);
}
}
curl_close ($ch);
echo $output;