PHP-使用两个站点之间的CURL获取返回值

PHP-使用两个站点之间的CURL获取返回值,php,curl,Php,Curl,我有两个PHP站点,一个是API站点(x.PHP),另一个是用CURL(y.PHP)调用API站点 x.php如下所示: if (isset($_POST['testconnection'])) { return "ok"; } $host = "https://there.is.the/x.php"; $command = array ( "testconnection" => "testconnection" ); $ch=curl_init($ho

我有两个PHP站点,一个是API站点(x.PHP),另一个是用CURL(y.PHP)调用API站点

x.php如下所示:

if (isset($_POST['testconnection'])) {
        return "ok";
}
$host = "https://there.is.the/x.php";
$command = array (
        "testconnection" => "testconnection"
);

$ch=curl_init($host);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($command));
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT,180);

$response = curl_exec($ch);
curl_close($ch);

var_dump($response);
而y.php是这样的:

if (isset($_POST['testconnection'])) {
        return "ok";
}
$host = "https://there.is.the/x.php";
$command = array (
        "testconnection" => "testconnection"
);

$ch=curl_init($host);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($command));
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT,180);

$response = curl_exec($ch);
curl_close($ch);

var_dump($response);
正如您在示例中所看到的,我希望从y.php中的x.php获取返回字符串,但在回答中得到一个空字符串:string(0)” 我想应该是:字符串(2)“ok” 我已经在x.php中替换了返回echo,但没有成功

抱歉,如果这是一个noob问题,我对curl很陌生。

替换

return "ok";

您的脚本正在退出并返回值,但没有任何内容作为响应输出。 需要注意的是,web服务器必须具有访问x.php的权限。验证脚本是否能够执行。权限问题通常会导致空白页

提示:使用Postman测试RESTAPI。您可以对x.php进行post查询,并查看返回的结果,从而消除错误的curl部分