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 调用函数时,无关变量的改变引起函数值的改变
  • ¥15 xy坐标转化为经纬度坐标
  • ¥15 一般三角模糊数的上界值和下届值取中值的多少比较合理?
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥20 Hbase启动失败,无法启动HMaster
  • ¥15 sqpingboot在父模块已经引入了mybatisplus 在子组件不写版本号还是报错
  • ¥20 Lumerical FDTD solutions 中模型的相对阻抗,有效介电常数和有效磁导率的实部和虚部的数据如何获得?
  • ¥100 sql reporting service 远程smtp服务器配置支持
  • ¥15 ppyoloe_r带角度目标检测,loss_cls没法收敛
  • ¥15 淘宝交易指数如何解读,其关联的数据指标是什么