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

时间:2021-07-27 16:17:14

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

我试图弄清楚如何通过PHP将这种数据发送到RESTful服务:

{
  "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.

首先,我不能使用curl,因为谁将要使用此代码可能没有安装它或可能不允许安装它。

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

这不是一个'正常'的JSON对象。 job-> file属性是一个实际文件(甚至不是文件的url)。

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

这是我用来发送所有请求的实际代码,它一直有效,直到我遇到这个具体案例:http://pastebin.com/6pEjhAkg

The 'file' property is created as such:

'file'属性是这样创建的:

$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?

现在,在发送数据时,在PHP端正确设置了$ params参数,RESTful会返回关于缺少'api_version'和'project_id'的错误,但它们存在。我很确定问题是他没有以JSON格式接收数据,但是如何在他的属性中将包含文件指针资源的对象转换为JSON?

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.

我试图理解那里有什么问题,到目前为止我唯一能解决的问题是,另一个不相关的问题是实际发送JSON数据(当$ multipart == false时),正如你在第16-19行中看到的那样。链接代码,而不是发送urlencoded数据。

Any hint?

1 个解决方案

#1


0  

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.

JSON没有“文件”概念。您可以加载文件内容,使用Base64对其进行编码,然后将其填充到JSON字符串中 - 或者您可以使用多部分格式在一个部分中发送文件,在另一部分中使用完整的JSON。多部分解决方案应该是最佳性能,因为它不必对文件内容进行base64编码。

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

以下是来自Mason的类似示例消息格式,它尝试形式化json / multipart用法:

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."
  }
}

#1


0  

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.

JSON没有“文件”概念。您可以加载文件内容,使用Base64对其进行编码,然后将其填充到JSON字符串中 - 或者您可以使用多部分格式在一个部分中发送文件,在另一部分中使用完整的JSON。多部分解决方案应该是最佳性能,因为它不必对文件内容进行base64编码。

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

以下是来自Mason的类似示例消息格式,它尝试形式化json / multipart用法:

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."
  }
}