doureng6738 2014-08-27 05:30
浏览 39
已采纳

试图用图片向bayimg发出一个php POST请求

Hi Im trying to automate the upload of pictures to imgbay... seeing the request that is generated from the browser with wireshark and seeing this post I came up with this.

$destination = "http://bayimg.com";


$eol = "
";
$data = '';




$str=file_get_contents('umbered1.jpg');

$mime_boundary='-------------------------'. time();

$data .= '--' . $mime_boundary . $eol;
$data .= 'Content-Disposition: form-data; name="file;"';
$data .= 'filename="umbered1.jpg"' . $eol;
$data .= 'Content-Type: image/jpeg' . $eol . $eol;
$data .= $str. $eol;
$data .= '--' . $mime_boundary . $eol;
$data .= 'Content-Disposition: form-data; name="code"' . $eol;
$data .= 'bla';
$data .= '--' . $mime_boundary . $eol;
$data .= 'Content-Type: form-data; name="tags"' . $eol;
$data .= '--' .$mime_boundary . $eol.$eol;


$params = array('http' => array(
                  'method' => 'POST /upload',
                  'protocol_version' => '1.1',
                  'header' => 'User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:31.0) Gecko/20100101 Firefox/31.0'. $eol .
                                'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'. $eol .
                                'Accept-Language: en-US,en;q=0.5' . $eol .
                                'Accept-Encoding: gzip, deflate' . $eol .
                                'Referer: http://bayimg.com/' . $eol . 
                                'Content-Type: multipart/form-data; boundary='. $mime_boundary. $eol .
                                 "Content-Length: " . strlen($data). $eol,
                  'content' => $data
               ));

$ctx = stream_context_create($params);
echo $response = @file_get_contents($destination, 0, $ctx);

what I see in wireshark is this

 POST /upload / HTTP/1.1

 Host: bayimg.com

 User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:31.0) Gecko/20100101 Firefox/31.0

 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

 Accept-Language: en-US,en;q=0.5

 Accept-Encoding: gzip, deflate

 Referer: http://bayimg.com/

 Content-Type: multipart/form-data; boundary=-------------------------1409116238

 Content-Length: 12681



 ---------------------------1409116238

 Content-Disposition: form-data; name="file;"filename="umbered1.jpg"

 Content-Type: image/jpeg

 //Here comes the image data....

 ---------------------------1409116238

 Content-Disposition: form-data; name="code"

 bla---------------------------1409116238

 Content-Type: form-data; name="tags"

 ---------------------------1409116238



 HTTP/1.1 200 OK

 Server: nginx/1.6.0

 Date: Wed, 27 Aug 2014 05:10:41 GMT

 Content-Type: text/html; charset=UTF-8

 Transfer-Encoding: chunked

 Connection: keep-alive

 X-Powered-By: PHP/5.6.0RC3

 Set-Cookie: country=AR; expires=Wed, 03-Sep-2014 05:10:41 GMT; Max-Age=604800

 Content-Encoding: gzip

// the response is an error page

///// end of packet

The server responds but its an error....

this is what I get when I upload the image via the web.

 POST /upload HTTP/1.1

 Host: bayimg.com

 User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:31.0) Gecko/20100101 Firefox/31.0

 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

 Accept-Language: en-US,en;q=0.5

 Accept-Encoding: gzip, deflate

 Referer: http://bayimg.com/

 Cookie: country=AR; __PPU_SESSION_4-7=X54717,1408841107,0,0X

 Connection: keep-alive

 Content-Type: multipart/form-data; boundary=---------------------------51442902223623

 Content-Length: 12721



 -----------------------------51442902223623

 Content-Disposition: form-data; name="file"; filename="umbered1.jpg"

 Content-Type: image/jpeg

 /// here comes the image data

 -----------------------------51442902223623

 Content-Disposition: form-data; name="code"



 bla

 -----------------------------51442902223623

 Content-Disposition: form-data; name="tags"





 -----------------------------51442902223623--

 HTTP/1.1 200 OK

 Server: nginx/1.6.0

 Date: Wed, 27 Aug 2014 05:18:50 GMT

 Content-Type: text/html; charset=UTF-8

 Transfer-Encoding: chunked

 Connection: keep-alive

 X-Powered-By: PHP/5.6.0RC3

 Content-Encoding: gzip

// the response is a error page

I see that I am not using the Cookies but I don't know how to set them. I also tried with curl but this is the closes that i get to replicate the header... any help will be appreciated. thanks!

  • 写回答

1条回答 默认 最新

  • dpauxqt1281 2014-08-27 06:12
    关注

    You are way over thinking this. Try this instead:

    <?php 
    
        $url  = 'http://bayimg.com/upload';
        $file = 'umbered1.jpg';
        $code = 'YourRemovalCode';
        $tags = 'Space Separated Tags';
    
        $data = array(
            'code' => $code,
            'tags' => $tags,
            'file' => '@' . $file
        );
    
        $ch = curl_init();
    
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    
        $response = curl_exec($ch);
    
    ?>
    

    To be honest, I didn't think they'd allow just anyone to POST images to the site, but apparently so. Enjoy!

    EDIT: If you are uising PHP 5.5 or greater, try this version:

    $url  = 'http://bayimg.com/upload';
    $file = 'umbered1.jpg';
    $code = 'YourRemovalCode';
    $tags = 'Space Separated Tags';
    
    // No need to change anything from here on.
    $mime = image_type_to_mime_type(exif_imagetype($file));
    $data = array(
        'code' => $code,
        'tags' => $tags,
        'file' => new CurlFile($file, $mime)
    );
    
    $ch = curl_init();
    
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    
    $response = curl_exec($ch);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?