Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending image using php cURL

Tags:

php

curl

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'

like image 487
user1652792 Avatar asked Nov 04 '22 16:11

user1652792


1 Answers

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)
like image 52
kirugan Avatar answered Nov 15 '22 06:11

kirugan