【问题标题】:Webservice not able to parse JSON Request from a PHP fileWebservice 无法解析来自 PHP 文件的 JSON 请求
【发布时间】:2015-09-02 07:03:16
【问题描述】:

我正在尝试访问网络服务。我已经在一个 android 应用程序中使用这个 web 服务,但现在我需要从一个 php 文档中访问一些函数。如果我使用 chromes 高级 rest 客户端应用程序进行测试,如果我选择 POST 选项和 application/x-www-form-urlencode 作为内容类型,它工作正常。但是当我尝试从我的 PHP 文件访问 web 服务时,我得到了服务器的响应,它找不到值“tag”。这是代码:

$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);

这段代码有什么问题?

感谢您的帮助!

【问题讨论】:

    标签: php json rest post


    【解决方案1】:

    试试这个:

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

    按照here的定义,“Content”值必须是字符串:http_build_query生成您需要的URL编码查询字符串。

    【讨论】:

    • 感谢您提供的信息。我试过http_build_query,但我的服务器端代码中的$_POST['tag']调用仍然是空的:(
    • 啊,我发现了错误:我的标题定义中有错字:类型的 T 应该很小,并且它是 urlencode 而不是 urlencode。这与http_build_query 一起工作!谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-14
    • 1970-01-01
    • 2021-10-19
    • 1970-01-01
    • 2021-07-08
    相关资源
    最近更新 更多