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

时间:2014-08-27 05:30:27

标签: php http post request

嗨我正在尝试自动上传图片以进行imgbay ...看到使用wireshark从浏览器生成的请求并看到这个post我想出了这个。

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


$eol = "\r\n";
$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);

我在wireshark中看到的是这个

 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

//响应是错误页面

/////数据包结束

服务器响应但是错误....

这是我通过网络上传图片时得到的结果。

 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

//响应是错误页面

我发现我没有使用Cookies,但我不知道如何设置它们。我也尝试过卷曲,但这是我要复制标题的关闭...任何帮助将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:1)

你是在想这个。试试这个:

<?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);

?>

说实话,我认为他们不允许任何人将图像发布到网站上,但显然是这样。享受!

  

编辑:如果您正在使用PHP 5.5或更高版本,请尝试以下版本:   

$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);