Pregunta

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?

¿Fue útil?

Solución

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."
  }
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top