简体   繁体   English

PHP post Rest API

[英]PHP post Rest API

I'm searching all day long what's happen with my api call from php page but can't find a working solution. 我整日都在搜索php页面中的api调用发生了什么,但找不到有效的解决方案。 Simple, I have one rest api which needs 2 parameters, it returns a json message. 很简单,我有一个rest api,它需要2个参数,它返回一个json消息。 Calling the api from postman for example, it works fine. 例如,从邮递员那里调用api,就可以正常工作。 Calling from my php page, it blocks my webserver until timeout raises. 从我的php页面调用,它将阻止我的网络服务器,直到超时。

my block is (one of found here on SO): 我的方块是(在此处找到的其中之一):

        $url = HOST . '/users/verifyemail';
        $userID = $_GET['userID'];
        $key = $_GET['key'];            

        $postdata = http_build_query(
            array(
                'userID' => $userID,
                'apiKey' => $key
            )
        );              

        try
        {

            $opts = array('http' =>
                array(
                    'method'  => 'POST',
                    'header'  => 'Content-Type: application/x-www-form-urlencoded',
                    'content' => $postdata
                )
            );
            $context  = stream_context_create($opts);

            $response = file_get_contents($url, false, $context);
            if ($response) {
                echo "OK";
            } else {
                echo "NOK !";
            }
        } catch (Exception $ex) {
            echo $ex;
        }  

Even the catch isn't called. 连捕获都没有。

In my webserver I have this: 在我的网络服务器中,我有以下内容:

[Wed Jun 17 20:39:18 2015] ::1:52312 [200]: /emailValidation.php?userID=16&key=1212
[Wed Jun 17 20:39:18 2015] ::1:52313 [200]: /users/verifyemail

As everything was fine... 一切都很好...

My PHPInfo 我的PHPInfo

Any idea ? 任何想法 ?

Thank you ! 谢谢 !

please try php-curl: 请尝试php-curl:

$curl = curl_init();
curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => "$url",
    CURLOPT_POST => 1,
    CURLOPT_POSTFIELDS => array(
        userID => "$userID",
        apiKey => "$key",
    )

));

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

phpinfo输出

In fact, Kasper Franz was right. 实际上,Kasper Franz是正确的。 Even having PHP installed as third part in my mac, it was using the built-in instance and the problem was about the single thread. 即使将PHP作为我的Mac中的第三部分安装,它也使用了内置实例,问题出在单线程上。

Changing the configuration resolved the issue. 更改配置解决了该问题。

Thank you AirPet for your prompt help too ;-) 也感谢您对AirPet的及时帮助;-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM