tags:

views:

77

answers:

2

Hello

Im trying to use imageshshack api in php

in the following way:

<?php
function do_post_request($url, $data, $optional_headers = null) {
  $params = array('http' => array(
              'method' => 'POST',
              'content' => $data
            ));
  if ($optional_headers !== null) {
    $params['http']['header'] = $optional_headers;
  }
  $ctx = stream_context_create($params);
  $fp = @fopen($url, 'rb', false, $ctx);
  if (!$fp) {
    throw new Exception("Problem with $url, $php_errormsg");
  }
  $response = @stream_get_contents($fp);
  if ($response === false) {
    throw new Exception("Problem reading data from $url, $php_errormsg");
  }
  return $response;
}

    $fileName = $_FILES['picture']['name'];
    $tmpName = $_FILES['picture']['tmp_name'];
    $fileSize = $_FILES['picture']['size'];
    $fileType = $_FILES['picture']['type'];
    $fo = @fopen($tmpName, "r");
    $imgposted = @fread($fo, filesize($tmpName));
    $imgposted = addslashes($imgposted);

    $data = 'fileupload='.$imgposted.'&rembar=1&key=******';
    echo do_post_request('http://www.imageshack.us/upload_api.php',$data);

?>

but the data is being posted normally, I wanted to be posted like if I had a form with this attribute:

enctype="multipart/form-data"

any solution?

A: 

do_post_request(
    'http://www.imageshack.us/upload_api.php',
    $data,
    'Content-Type: multipart/form-data')
);

racetrack
you cheater :-P
aularon
@racetrack:thanks, but this error appears:Fatal error: Uncaught exception 'Exception' with message 'Problem with http://www.imageshack.us/upload_api.php, ' in /var/www/html/test/imageshack/rec.php:14 Stack trace: #0 /var/www/html/test/imageshack/rec.php(31): do_post_request('http://www.imag...', 'fileupload=?PNG...', Array) #1 {main} thrown in /var/www/html/test/imageshack/rec.php on line 14
el7r
@el7r Updated the answer, try the new one (replacing the array, with string).
racetrack
@racetrack:it returned as it was before,imageshack saysSorry, but we've detected that unexpected data is received. Required parameter 'fileupload' is missing or your post is not multipart/form-data
el7r
@el7r shouldn't `$imgposted` be base64 encoded?
racetrack
@racetrack no, I tried a simple html form and it worked correctly and the photo has been uploaded , but I need to do it through html and get the response
el7r
A: 

You need to set it as a header:

$optional_headers = array(
  'Content-Type'=>'multipart/form-data'
);

When calling the function:

do_post_request('http://www.imageshack.us/upload_api.php', array(
   'Content-Type'=>'multipart/form-data'
));

EDIT: I think you also have to url_encode rather than doing addslashes.

aularon
I tried urlencode, still its not sent as 'multipart/form-data'
el7r