简体   繁体   English

使用php cURL发送图像

[英]Sending image using php cURL

Hello i need to send image using PHP cURL, tried to do it like this: 您好我需要使用PHP cURL发送图像,尝试这样做:

$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. 当然我试图用这个代码做普通的POST请求,它工作正常,只是当我尝试发布图像它不起作用。

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: 我通过LIVE HTTP HEADERS捕获了标题,它看起来像是:Theese是我用mozilla和addon捕获的正确标题,所以cURL应该做同样的事情:

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' 还试图在'@ / home / me / images / something.jpg'之后添加; filename =“something.jpg”和; type =“image / jpeg”

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)

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); 并改变了这个: curl_setopt($this->ch, CURLOPT_POSTFIELDS, $postStr); into curl_setopt($this->ch, CURLOPT_POSTFIELDS, $postValues); curl_setopt($this->ch, CURLOPT_POSTFIELDS, $postValues);

Postfields need to be an array to get "Content-Type: multipart/form-data". Postfields需要是一个数组才能获得“Content-Type:multipart / form-data”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM