5

Hello i need to send image using PHP cURL, tried to do it like this:

$this->ch = curl_init();
curl_setopt($this->ch, CURLOPT_URL, $URL);
$postValues = array(
            'utf8' => $utf8,
            'authenticity_token' => $auth_token,
            'media_object[title]' => 'something',
            'media_object[source]' => '',
            'media_object[object_type]' => 0,
            'media_object[image]'=>'@/home/me/images/something.jpg',
            'captcha' => $captcha,
        );

$n = 0;
        $postStr = '';
        foreach ($postValues as $name => $value) {
            if ($n == 0)
                $postStr .= $name . '=' . $value;
            else
                $postStr .= '&' . $name . '=' . $value;
            $n++;
        }
        curl_setopt($this->ch, CURLOPT_URL, $url);
        curl_setopt($this->ch, CURLOPT_POSTFIELDS, $postStr);
        curl_setopt($this->ch, CURLOPT_POST,1);
curl_setopt($this->ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 5.1; rv:14.0) Gecko/20100101 Firefox/14.0.1");
        curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($this->ch, CURLOPT_COOKIEFILE, TMP.'cookies.txt');
        curl_setopt($this->ch, CURLOPT_COOKIEJAR, TMP.'cookies.txt');
curl_exec($this->ch);

But it doesn't work. Of course i tried to do ordinary POST request with this code and it works fine, just when i try to post image it doesn't work.

I've captured headers via LIVE HTTP HEADERS and it looks like that: Theese are correct headers that i've captured using mozilla with addon, so cURL shoud do the same thing:

Content-Type: multipart/form-data; boundary=---------------------------41184676334
Content-Length: 218115
-----------------------------41184676334
Content-Disposition: form-data; name="utf8"

â
-----------------------------41184676334
Content-Disposition: form-data; name="authenticity_token"

0Je50mUpOMdghYgHPH5Xjd8UnbuvzzQLyyACfvGmVgY=
-----------------------------41184676334
Content-Disposition: form-data; name="media_object[title]"

Something
-----------------------------41184676334
Content-Disposition: form-data; name="media_object[source]"


-----------------------------41184676334
Content-Disposition: form-data; name="media_object[object_type]"

0
-----------------------------41184676334
Content-Disposition: form-data; name="media_object[image]"; filename="something.jpg"
Content-Type: image/jpeg

ÿØÿà

Is there anything wrong or what ? Webpage that im trying to send image returns message "File name can't be empty!" Also tried to add ;filename="something.jpg" and ;type="image/jpeg" after '@/home/me/images/something.jpg'

1
  • Shouldn't there be an extra newline between the headers and the body? Commented Sep 6, 2012 at 17:56

2 Answers 2

2

I have similar problem a few weeks ago, try to change location of file or permissions to it. To find out which headers curl sending try this:

curl_setopt(CURLINFO_HEADER_OUT, true);

after request retrieve information about headers:

curl_getinfo($this->ch, CURLINFO_HEADER_OUT)
Sign up to request clarification or add additional context in comments.

Comments

0

Solved my problem by removing this part:

$n = 0;
        $postStr = '';
        foreach ($postValues as $name => $value) {
            if ($n == 0)
                $postStr .= $name . '=' . $value;
            else
                $postStr .= '&' . $name . '=' . $value;
            $n++;
        }

And changed this: curl_setopt($this->ch, CURLOPT_POSTFIELDS, $postStr); into curl_setopt($this->ch, CURLOPT_POSTFIELDS, $postValues);

Postfields need to be an array to get "Content-Type: multipart/form-data".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.