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.

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

报告相同问题?