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 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上