使用php cURL发送图像

使用php cURL发送图像,php,curl,Php,Curl,您好,我需要使用PHP cURL发送图像,尝试如下操作: $this->ch = curl_init(); curl_setopt($this->ch, CURLOPT_URL, $URL); $postValues = array( 'utf8' => $utf8, 'authenticity_token' => $auth_token, 'media_object[title]' => 's

您好,我需要使用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);
但它不起作用。当然,我试着用这段代码做普通的POST请求,它工作得很好,只是当我试着发布图像时,它不工作

我已经通过LIVE HTTP标头捕获了标头,看起来是这样的: 这些都是我使用mozilla with 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

ÿØÿà
有什么不对劲吗?我试图发送图像的网页返回消息“文件名不能为空!”
还试图补充,;filename=“something.jpg”和;type=“image/jpeg”在“@/home/me/images/something.jpg”之后

几周前我遇到类似问题,请尝试更改文件的位置或权限。要找出要发送的标题,请尝试以下操作:

curl_setopt(CURLINFO_HEADER_OUT, true);
请求后检索有关标头的信息:

curl_getinfo($this->ch, CURLINFO_HEADER_OUT)

通过删除此部件解决了我的问题:

$n = 0;
        $postStr = '';
        foreach ($postValues as $name => $value) {
            if ($n == 0)
                $postStr .= $name . '=' . $value;
            else
                $postStr .= '&' . $name . '=' . $value;
            $n++;
        }
并改变了这一点:
curl\u setopt($this->ch,CURLOPT\u POSTFIELDS,$postStr)进入
curl\u setopt($this->ch,CURLOPT\u POSTFIELDS,$postValues)


Postfields需要是一个数组才能获取“内容类型:多部分/表单数据”。

标题和正文之间不应该有额外的换行符吗?