3

I am having some trouble uploading files to Imageshack's API. I use a multipart/form-data form to get the file.

index.php:

<form method="post" enctype="multipart/form-data" action="upload.php">
    <input type="file" name="fileupload"/>
    <input type="submit" value="Go"/>
</form>

Normally I would have no problem with this, however the data must be sent to http://imageshack.us/upload_api.php and the response is given back in an XML styled HTML page on their server so there's really nothing I can do with it. So I decided to pass the form through a PHP cURL script and getting the response on the same page.

upload.php:

<?php
    $url = 'http://imageshack.us/upload_api.php';
    $key = '4BEILRTV5ff57ecb70867e8becb2c4b5e695bdb4';
    $max_file_size = '5242880';
    $temp = $_FILES["fileupload"]["tmp_name"];
    $name = $_FILES["fileupload"]["name"];

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_VERBOSE, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);

    $post = array(
        "fileupload" => '@' . $temp,
        "key" => $key,
        "max_file_size" => $max_file_size
    );
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    $response = curl_exec($ch);
    echo $response;
?>

At first I was getting a bunch of errors, but now I don't get any response. Not even an error.

Any suggestions on using this method would be great!

eliwedel
  • 1,363
  • 2
  • 10
  • 12

1 Answers1

1

I had to include "format" => 'json' into the post options and use json_decode to parse the info. Here's the upload.php script.

<?php
    $url = 'http://imageshack.us/upload_api.php';
    $key = '4BEILRTV5ff57ecb70867e8becb2c4b5e695bdb4';
    $max_file_size = '5242880';
    $temp = $_FILES["fileupload"]["tmp_name"];

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_URL, $url);

    $post = array(
        "fileupload" => '@' . $temp,
        "key" => $key,
        "format" => 'json',
        "max_file_size" => $max_file_size,
        "Content-type" => "multipart/form-data"
    );
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    $response = curl_exec($ch);
    $json_a=json_decode($response,true);
    echo $json_a[links][image_link];
?>
eliwedel
  • 1,363
  • 2
  • 10
  • 12