doudeng8691 2017-03-17 12:30
浏览 49
已采纳

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

I have two PHP sites, an API site (x.php) and another, which calls the API site with CURL (y.php)

The x.php looks like this:

if (isset($_POST['testconnection'])) {
        return "ok";
}

And the y.php like this:

$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);

As you can see in the example, I would like to get the return string from x.php in the y.php, but I get an empty string in the answer: string(0) "" I think it would be: string(2) "ok" I have replaced the return to echo in x.php but without success.

Sorry if it is a noob question, I'm quite new in curl.

  • 写回答

1条回答 默认 最新

  • dtef9322 2017-03-17 13:02
    关注

    Replace

    return "ok";
    

    With

    echo "ok";
    

    Your script is exiting with return value, but nothing is output as a response. A thing to note is that web server must have permissions to access the x.php. Verify that the script is able to execute. Permission problems often result in a blank page.

    Tip: Use Postman to test REST APIs. You could shoot the post query against your x.php and see what comes back, eliminating the curl part being wrong.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵