duanliang1999 2014-03-12 10:12
浏览 122
已采纳

使用文件指针资源发送到RESTful服务JSON

I'm trying to figure out how to send this kind of data, via PHP, to a RESTful service:

{
  "api_version": 1.0,
  "project_id" : 123,
  "batch_id" : 111,
  "accesskey" : "abc123",
  "job": {
    "file": file,
    "word_count": 111,
    "title": "FAQ",
    "cms_id": "page_123_en_sp",
    "url" : "http://original_post_url.com",
    "is_update": false, 
    "translator_id": 123,
    "note": "Translate these words",
    "source_language": "en",
    "target_language": "it"
  }
}

First of all, I cannot use curl, as who's going to use this code might not have it installed or might be not allowed to install it.

That's not a 'normal' JSON object. The job->file property is an actual file (not even an url to a file).

That's the actual code I'm using to send all requests and it worked, until I've met this specific case: http://pastebin.com/6pEjhAkg

The 'file' property is created as such:

$file = generate_file( $content );

protected static function generate_file($content) {
    $file = fopen('php://temp','r+');
    fwrite($file, $content);
    rewind($file);
    return $file;        
}

Now, when sending data, with $params argument properly set on PHP side, the RESTful returns an error about missing 'api_version' and 'project_id', but they are present. I'm pretty sure the problem is that he's not receiving data as JSON, but how can I convert to JSON an object that, in his properties, contains a file pointer resource?

The code that sends data and builds the file has been created by a former developer, and I can't get in touch with him.

I tried to understand what is wrong there and the only thing I managed to fix so far, for another unrelated issue, is to actually send JSON data (when $multipart==false), as you can see in lines 16-19 of the linked code, rather than sending urlencoded data.

Any hint?

  • 写回答

1条回答 默认 最新

  • douchenbiao0916 2014-03-12 11:31
    关注

    JSON does not have a "file" concept. You can either load the file content, encode it using Base64 and then stuff it into a JSON string - or you can use multipart format for sending the file in one part and the complete JSON in another part. The multipart solution should be the best performing as it doesn't have to base64 encode the file content.

    Here is a similar example message format from Mason that tries to formalize the json/multipart usage:

    POST /projects/2/issues HTTP/1.1
    Accept: application/vnd.mason+json
    Content-Type: multipart/form-data; boundary=d636dfda-b79f-4f29-aaf6-4b6687baebeb
    
    --d636dfda-b79f-4f29-aaf6-4b6687baebeb
    Content-Disposition: form-data; name="attachment"; filename="hogweed.jpg"
    ... binary data for attached image ...
    
    --d636dfda-b79f-4f29-aaf6-4b6687baebeb
    Content-Disposition: form-data; name="args"; filename="args"
    Content-Type: application/json
    
    {
      "Title": "Hogweeds on the plaza",
      "Description": "Could you please remove the hogweeds growing at the plaza?",
      "Severity": 5,
      "Attachment":
      {
        "Title": "Hogweed",
        "Description": "Photo of the hogweeds."
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 链接问题 C++LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接