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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀