我有以下json字符串http://pastebin.com/wnkiLqvP,我试图将其发布到betaface API。该帖子的PHP在卷曲完成,那确实帖子的功能是:使用json字符串向API发送请求时出错
function bcall_endpoint($endpoint,$json_params){
//Init CURL
$ch = curl_init($endpoint);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_params);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$result = curl_exec($ch);
return $result;
}
此外,在引擎收录文件中的JSON字符串由下面的一段代码生成:
$beta = array(
'api_key'=>'d45fd466-51e2-4701-8da8-04351c872236',
'api_secret'=>'171e8465-f548-401d-b63b-caf0dc28df5f',
'imagefile_data'=>$base64,
'detection_flags'=>'cropface,recognition,propoints,classifiers,extended'
);
$beta = json_encode($beta);
哪里$ base64是一个文件的base64字符串。基于64位字符串创建类似如下:
$file = file_get_contents($_FILES['file']['tmp_name']);
$base64 = base64_encode($file);
的API终点是在这里http://www.betafaceapi.com/service_json.svc/UploadNewImage_File这个版本的API与JSON工作。当我尝试将所有内容发送到端点时,我应该回复一个响应。 Rathar比反应,我居然得到这样的卷曲后的所有PARAMS后如下:
HEADERS
Access-Control-Allow-Origin: *
Cache-Control: private
Content-Length: 1110
Content-Type: text/html
Date: Wed, 22 Jan 2014 07:55:32 GMT
Server: Microsoft-IIS/7.5
X-Aspnet-Version: 4.0.30319
X-Powered-By: ASP.NET
X-Powered-By-Plesk: PleskWin
BODY view raw
<html version="-//W3C//DTD XHTML 2.0//EN" xml:lang="en" xsi:schemaLocation="http://www.w3.org/1999/xhtml http://www.w3.org/MarkUp/SCHEMA/xhtml2.xsd"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<HEAD>
<TITLE>Request Error</TITLE>
</HEAD>
<BODY>
<DIV id="content">
<P class="heading1">
<B>Error Status Code:</B> 'InternalServerError'
</P>
<P>
<B>Details: </B>There was an error deserializing the object of type BetafaceWebService.ImageRequestBinary. End element 'imagefile_data' from namespace '' expected. Found text '/'.
</P>
<!-- Padding xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-->
</DIV>
</BODY>
</html>
可有人请点我,为什么我得到这个错误:有一个错误反序列化类型BetafaceWebService的对象.ImageRequestBinary。从命名空间''预期结束元素'imagefile_data'。找到文字'/'。 ?
看来,如果我上传文件,请在API端点上运行POST,PHP响应为NULL。但是,如果我查看页面的源代码,并再次点击刷新,然后重新发送表单提交,我会从API获取propper响应。
有人可以让我知道这里有什么问题吗?自昨天以来我一直在努力了解这一点,并找不到解决方案。
更新:使用jsonlint与JSON字符串PHP生成给我,我得到以下结果 后:
Parse error on line 4:
... "imagefile_data": "\/9j\/4AAQSkZJRgABA
-----------------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '['
是否有JSON字符串大小限制任何?
您应该隐藏您的api秘密。 – user658991
这是一个公开的秘密:)这不是问题。但感谢您的意愿 – roshkattu