我正在尝试从教程中学习如何在php中使用curl。我有一个PHP脚本与另一个PHP通信。第一个脚本是:
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,"http://localhost/some_directories/testing.php");
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, "Hello=World&Foo=Bar&Baz=Wombat");
curl_exec ($curl);
curl_close ($curl)
第二个'testing.php'脚本只包含php标签和:var_dump($_POST);
当我运行第一个脚本时,我得到输出:var_dump($_POST);
而不是发布的值。我确信这是显而易见的,但我不确定为什么会发生这种情况。
答案 0 :(得分:1)
<?php
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,"http://localhost/some_directories/testing.php");
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, "Hello=World&Foo=Bar&Baz=Wombat");
$response = curl_exec ($curl);
curl_close ($curl);
echo $response;
?>
<强> testing.php 强>
<?php
var_dump($_REQUEST);
检查一下......如果你的PHP代码都没有运行 - https://stackoverflow.com/a/5121589/781251
答案 1 :(得分:0)
您忘了为CURLOPT_RETURNTRANSFER
试试这个,它更清洁,并且稍微破坏你的参数
$url = 'http://localhost/some_directories/testing.php';
$ch = curl_init($url);
curl_setopt_array($ch, array(
CURLOPT_POST => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_POSTFIELDS => http_build_query(array(
'Hello' => 'World',
'Foo' => 'Bar',
'Baz' => 'Womabt',
)),
));
// error
if (FALSE === ($data = curl_exec($ch)))
{
print_r(curl_error($ch));
}
curl_close($ch);
print_r($data);