Hello FreeBDevs:<br><br>So we finally got over the hump here; we fired up 'tcpflow' (awesome network diagnostic tool, by the way, easier than tcpdump) to monitor the exchange between the local system and freebase.<br>
<br>-The Broken-<br>Freebase's 'raw' upload url (<a href="http://sandbox.freebase.com/api/service/upload">sandbox.freebase.com/api/service/upload</a>) only accepts 'POST' requests but doesn't know how to deal with multipart/form-data as a content type. PHP/libcurl defaults its POST to Content-Type: multipart/form-data, and embeds the image inside of that multipart envelope. Below is the tcpflow output with the broken code, minus some extraneous stuff (
<a href="http://192.168.0.150">192.168.0.150</a> is the local system, <a href="http://208.068.108.121">208.068.108.121</a> is <a href="http://sandbox.freebase.com">sandbox.freebase.com</a>):<br><br>192.168.000.150.46220-208.068.108.121.00080
: POST /api/service/upload HTTP/1.1<br>Content-Type: image/gif<br>Content-Length: 13365<br>Content-Length: 13555<br>Expect: 100-continue<br><br>192.168.000.150.46220-208.068.108.121.00080: Content-Type: multipart/form-data; boundary=----------------------------b8245f93a0cb
<br>------------------------------b8245f93a0cb<br>Content-Disposition: form-data; name="filename"; filename="CORAL.gif"<br>Content-Type: image/gif<br><br><br>192.168.000.150.46220-208.068.108.121.00080
: GIF89a+.........).X.T.r....-.w.\.r....1{..d>3o;..5wV.l~3..,g.....;<Image Data Here><br>192.168.000.150.46220-208.068.108.121.00080: <br>------------------------------b8245f93a0cb--<br><br>208.068.108.121.00080-192.168.000.150.46220
: HTTP/1.0 400 Bad Request<br><br><br>***This lead to the cryptic "cannot identify image file" response from the freebase server.***<br><br><br>-The Fix-<br>Straightforward, but there are no examples of this I could find in Google to use CURLOPT_READDATA instead of CURLOPT_POSTFIELDS:
<br><br>Replace one line:<br> curl_setopt($request, CURLOPT_POSTFIELDS,$content);<br><br>With these two lines, dropping the @ from the front of the file name:<br> $fp = fopen ($content, "r"); <br> curl_setopt($request, CURLOPT_READDATA, $fp);
<br><br><br>-The New-<br>The working interaction is as follows, note there is no mention of multiparts:<br><br>192.168.000.150.59161-208.068.108.121.00080: POST /api/service/upload HTTP/1.1<br>Content-Type: image/gif<br>Content-Length: 13365
<br>Expect: 100-continue<br><br><br>192.168.000.150.59161-208.068.108.121.00080: GIF89a+............,.<Image Data Here><br>\.r....1{..d>3o;..5wV.l~3..,g.....;<br>208.068.108.121.00080-192.168.000.150.59161: HTTP/1.0 200 OK
<br><br><br>Hope this leads to less pulled out hair for someone.<br><br>Regards,<br>Indy<br><br><br><div class="gmail_quote">On Dec 30, 2007 2:05 AM, Coral Link <<a href="mailto:corallink@gmail.com">corallink@gmail.com
</a>> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">Hi; I'm using PHP/LibCURL to upload images relevant to my topics. I'm guessing its more of a PHP issue than Freebase, but hopefully someone will indulge a helping hand.
<br><br>I'm running into this problem when uploading an image:
<br><br>{
"status": "400 Application Error",
"code": "/api/status/error",
"messages": [
{
"info": {
"exception": "cannot identify image file"
},
"message": "Invalid image file.",
"code": "/api/status/error/upload/invalid_image_data"
}
]
}<br><br>When I run the following:<br>$content="@/hole/www/corallink.com/freebase/files/uwlImage/9202a8c04000641f80000000060b61a7/CORAL.gif";<br>$type="image/gif";<br><br>$request = curl_init($url);<br>
curl_setopt($request, CURLOPT_COOKIEFILE, $cookiejarfile); //get some cookies<br>curl_setopt($request, CURLOPT_POST, 1);<br>curl_setopt($request, CURLOPT_HTTPHEADER, array("X-Metaweb-Request: TRUE", "Content-Type: ".$type));
<br>curl_setopt($request, CURLOPT_POSTFIELDS,$content);<br><br>// Return the result instead of printing it out.<br>curl_setopt($request, CURLOPT_RETURNTRANSFER, TRUE);<br>curl_setopt($request, CURLOPT_COOKIE, $credentials);
<br> <br>// Now fetch the URL<br>$responsetext = curl_exec($request);<br>curl_close($request);<br><br><br>This same code works great for uploading documents for the description in /common/topic. I've tried variations where I've made $content=array("file"=>"@
filename.gif"), which uploaded data, but Freebase tagged it as media_type 'octet/stream'. Any suggestions will be appreciated.<br><br>Thank you and kind regards,<br>Indy<br>
</blockquote></div><br>