上传图片,curl + multipart / form-data

时间:2016-06-29 09:10:12

标签: php image curl

我需要您对multipart / form-data的帮助以及使用curl上传图片。

我有这个代码用于生成和发送数据:

        $boundary = '----WebKitFormBoundaryLZI2dppfUIcXxqT0';
        $eol = "\r\n";

        $postdata = '';
        $postdata .= '--'.$boundary.$eol;
        $postdata .= 'Content-Disposition: form-data; name="scrid"'.$eol.$eol;
        $postdata .= $scrid.$eol;
        $postdata .= '--'.$boundary.$eol;
        $postdata .= 'Content-Disposition: form-data; name="file"; filename="'.$filepath.'"'.$eol;
        $postdata .= "Content-Type: {$imginfo['mime']}".$eol.$eol;
        $postdata .= $img.$eol;
        $postdata .= '--'.$boundary.'--';

        $headers = array(
            "Content-Length: " . strlen($postdata),
            "Content-Type: multipart/form-data;boundary=----WebKitFormBoundaryLZI2dppfUIcXxqT0",
            "X-Requested-With: XMLHttpRequest",
            "Origin:http://www.ebayclassifieds.com",
            "Accept-Encoding: gzip, deflate",
            "Accept:application/json, text/javascript, */*;",
        );

        curl_setopt($this->_ch, CURLOPT_URL, 'http://www.ebayclassifieds.com/m/ImageUpload');
        curl_setopt($this->_ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($this->_ch, CURLOPT_POSTFIELDS, $postdata);
        curl_setopt($this->_ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($this->_ch, CURLOPT_POST, 1);
        curl_setopt($this->_ch, CURLOPT_COOKIESESSION, TRUE);
        curl_setopt($this->_ch, CURLOPT_COOKIEJAR, $this->_cookieFilePath);
        curl_setopt($this->_ch, CURLOPT_COOKIEFILE, $this->_cookieFilePath);
        curl_setopt($this->_ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($this->_ch, CURLOPT_REFERER, "http://www.ebayclassifieds.com/m/PostAd?scrid=$scrid");

        $imageForm =  curl_exec($this->_ch);

我有请求有效载荷:

          ------WebKitFormBoundaryibLm7G5cqxCOuAFy
          Content-Disposition: form-data; name="file"; filename="1-g-0-032-   oz-silver-valcambi-bullion-bar-999-rev.jpg"
          Content-Type: image/jpeg

          Here appears the souce of the image itself like "ÿØÿàÿØÿàÿØÿàÿØÿàÿØÿàÿØÿà"
          ------WebKitFormBoundaryibLm7G5cqxCOuAFy
          Content-Disposition: form-data; name="scrid"

          32482346-7100587438898460646
          ------WebKitFormBoundaryibLm7G5cqxCOuAFy--

我如何获得图像来源? 我尝试获取源代码:$imgSource = file_get_contents($filepath),但在发送此文本后,服务器返回错误417 。 如果为图像发送无效的parament,则服务器返回带有错误消息的json请求(通常为此)

1 个答案:

答案 0 :(得分:0)

解决。 需要在标题中发送Expect: 0