tags:

views:

621

answers:

3

I am trying to use YFrog's API. I think I am using it correctly to try and send a HTTP POST request that is of content type XML. However, as the response body, I am always getting a 404 error for some reason. Here is their API: http://yfrog.com/upload_and_post.php

Here is my attempt of PHP code to upload.

 $data = array('media' => 'http://img253.imageshack.us/my.php?image=bfab82a545d414uo.jpg', 'username' => 'asc', 
    'password' => 'asc', 'message' => 'hi');

$url = 'http://yfrog.com/api/uploadAndPost';

$req = new HTTPRequest($url);
$req->addHeaders(array("Content-Type" => "text/xml")); 
$req->setMethod(HTTP_METH_POST); 
$req->addQueryData($data); $req->send();
echo $req->getResponseBody();

Also for the media it says "Binary image data" does that mean a URL of the image location can not be passed to it? What does it mean?

Thank you for any help?

+1  A: 

I think that the problem is basically that ImagesHack does not return a binary file when you give the URL posted above. For me, it returns an HTML document.

I think you should try passing a binary file as the "media" parameter. The photo that your trying to get is located at http://img253.imageshack.us/img253/172/bfab82a545d414uo.jpg, use this URL instead.

This is the response that I get with the one you originally tried:

HEAD /my.php?image=bfab82a545d414uo.jpg HTTP/1.1
Host: img253.imageshack.us

HTTP/1.1 200 OK
X-Powered-By: PHP/5.2.6
Set-Cookie: nopopunder=1; expires=Thu, 07-May-2009 11:10:21 GMT; path=/; domain=.imageshack.us
X-UA-Compatible: IE=EmulateIE7
Content-type: text/html
MC
You are right, I should be using that correct direct location of the image. I have made that change but I still get the 404 error as a response?!
Abs
A: 

You need to use addPostFields and addPostFile instead of addQueryData. An example of how to use both is here

@MC is correct in that you can't send along a URL pointer to a file somewhere else, you need to actually be uploading those bytes. So you'll want to download a local copy first, passing the path to your local copy of that file to the addPostFile method.

great_llama
A: 

You need to use a Content-Type of multipart/form-data.

yfrog sends back an XML file as a response, but the API is expecting a standard HTTP POST request with the image data.

The API docs state:

"post data should be formatted as multipart/form-data"

jim0thy