Web服务无法从PHP文件解析JSON请求

时间:2015-09-02 07:03:17

标签: php json rest post

我试图访问网络服务。我已经在Android应用程序中使用这个web服务,但现在我需要从php文档访问一些功能。如果我使用chromes高级rest客户端应用程序进行测试,如果我选择POST选项并将application/x-www-form-urlencode选为内容类型,则可以正常工作。但是当我尝试从我的PHP文件访问web服务时,我从服务器得到它无法找到值"标记"的响应。这是代码:

$data = array( 'tag' => 'something');

$options = array('http' => array(
'method' => 'POST',
'content' => $data,
'header' => "Content-Type: application/x-www-form-urlencode")
);

$context = stream_context_create($options);
$url = 'myurl';
$result = file_get_contents($url,false,$context);
$response = json_decode($result);

此代码有什么问题?

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

试试这个:

$data = http_build_query( array( 'tag' => 'something') );

根据定义here,“内容”值必须是字符串: http_build_query 生成您需要的URL编码查询字符串。

相关问题