【发布时间】:2015-06-17 18:48:00
【问题描述】:
我整天都在搜索从 php 页面调用我的 api 发生了什么,但找不到有效的解决方案。 很简单,我有一个需要 2 个参数的 rest api,它返回一条 json 消息。 例如,从邮递员那里调用 api,它工作正常。 从我的 php 页面调用,它会阻塞我的网络服务器,直到超时。
我的块是(在 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;
}
甚至不调用 catch。
在我的网络服务器中,我有这个:
[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
因为一切都很好......
有什么想法吗?
谢谢!
【问题讨论】:
-
你的常量 HOST 是什么,$url 的“输出”是什么
-
HOST = 'localhost:8000. $url = localhost:8000/users/verifyemail'
-
是的。甚至放置我的 index.php/.完全没有错误。
-
你是在命令行中使用 php 构建,而不是使用 apache 或类似的东西
标签: php rest post restful-url