使用cURL保存的图像已损坏 - 可能导致此问题?

时间:2014-07-02 15:18:14

标签: php forms curl

我编写了在网站上注册的代码。完整的代码在最后,但在本节中:

curl_setopt ($ch, CURLOPT_URL, $t3);
curl_setopt ($ch, CURLOPT_REFERER, $ref);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt ($ch, CURLOPT_HTTPGET, TRUE);
curl_setopt ($ch, CURLOPT_POST, FALSE);
curl_setopt ($ch, CURLOPT_USERAGENT, $agent);
curl_setopt ($ch, CURLOPT_BINARYTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookie);

$image = curl_exec($ch);
$path2  = $path.rand(1, 999999999).'.jpg';
file_put_contents($path2, $image);

它从网站抓取图像文件,但没有正确保存。图像被破坏了。我尝试了网上所有可用的东西,但它似乎没有用。 任何人都能说出问题所在吗?

完整代码是:

<?php
function getBetween($content,$start,$end)                   
    {                                                       
        $r = explode($start, $content);
        if (isset($r[1]))
            {
                $r = explode($end, $r[1]);
                return $r[0];
            }
        return '';
    }

ini_set('max_execution_time', 300);
global $agent;
$name    =  __FILE__;
$cpath   =  str_replace("am.php", "cookie/", $name);
$cookie  =  $cpath.'and'.rand(1,999999999).'.txt';

$ch    = curl_init();
$t1    = 'http://www.amulyam.in/signup.do?id=859958b9-b5f6-44f0-aa0f-ea2b02211659';
$agent = 'Mozilla/5.0 (Windows NT 6.1; rv:30.0) Gecko/20100101 Firefox/30.0';
$r1    = '';

curl_setopt($ch, CURLOPT_URL, $t1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
curl_setopt($ch, CURLOPT_POST, FALSE);

$d1 = curl_exec($ch);

$t2 = 'http://www.amulyam.in/?home';
curl_setopt($ch, CURLOPT_URL, $t2);
curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
curl_setopt($ch, CURLOPT_POST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);

$d2 = curl_exec($ch);

$start_t  = 'TOKEN" value="';
$end_t    = '"></div>';
$token = getBetween($d2, $start_t, $end_t);

$t3  = 'http://www.amulyam.in/signupCaptcha.jsp';
$ref = $t2; 

curl_setopt ($ch, CURLOPT_URL, $t3);
curl_setopt ($ch, CURLOPT_REFERER, $ref);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt ($ch, CURLOPT_HTTPGET, TRUE);
curl_setopt ($ch, CURLOPT_POST, FALSE);
curl_setopt ($ch, CURLOPT_USERAGENT, $agent);
curl_setopt ($ch, CURLOPT_BINARYTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookie);

$image = curl_exec($ch);
$path  = './img/'.rand(1, 999999999).'.jpg';
file_put_contents($path, $image);

echo    '<html>
            <head>
                <title>Amul Eazy Registration</title>
                <style type="text/css">
                    body {

                        background-color: #22274f;
                        text-align: center;
                    }

                    div.main {
                        text-align: center;
                        padding-left: 400px;

                    } 

                    div.block {
                        text-align: center;
                        height: 250px;
                        width: 468px;
                        background-color: #14193c;
                        color: white;
                        font-family: Tahoma, Geneva, sans-serif;
                        font-size: 20px;
                        font-weight: 400;
                        padding-top: 40px;




                    }

                    div {

                        margin: 10px;
                        padding-bottom: 10px;
                        width: 300px;
                        padding-left: 50px;
                    }

                    .title {
                        float: left;
                        width: 140px;
                        text-align: left;
                        padding-right: 10px;
                    }

                    div.header {

                        height: 50px;
                        padding-left: 150px;
                        padding-top: 30px;              
                    }

                    div.logo {

                        height: 44px;
                        width: 199px;
                        background-image: url("amul.png");
                        background-repeat: no-repeat;


                    }

                    div.credits {
                        height: 100px;
                        padding-left: 50px;
                    }

                    div.image {
                        float: right;
                    }
                    div.captcha{
                        background-image: url("'.$path.'");
                        background-repeat: no-repeat;

                    }
                    p.cred {
                        font-size: 30px;
                        color: #0083aa;
                        font-family: Georgia, Times, serif;
                        text-shadow: 2px 2px 7px #111111;

                    }

                    input[type=submit] {
                            width: 288px;
                            height: 45px;
                            background: url("create-account-button.png") no-repeat scroll 0% 0% transparent;
                            border: 0px none;
                            cursor: pointer;
                            outline: medium none;
                            font-family: "AccordAlternateBold";
                            font-size: 22px;
                            font-weight: 800;
                            text-shadow: 1px 1px rgba(0, 0, 0, 0.2);
                            line-height: 40px;
                            color: #FFF;
                            text-align: center;
                                                        }

                    input[type=submit]:hover {
                            width:288px;
                            height:45px;
                            background:url("create-account-button.png") 0 -50px;
                        }

                </style>
            </head>

            <body>  
                <div class="main">
                    <center>
                        <div class="header">
                            <div class="logo"></div>    
                        </div>
                        <form action="action.php" method="post">
                            <div class="block"> 
                                <div >
                                    <label for="mn" class="title">Mobile Number</label>
                                    <input type="text" id="mn"    name="mn" />
                                </div>
                                <div >
                                    <label for="ped" class="title">Password</label>
                                    <input type="text" id="pwd"   name="pwd" />
                                </div>
                                <div >                          
                                    <label for="email" class="title">Email</label>
                                    <input type="text" id="email" name="email" />
                                </div>

                            <div>
                                <div class="captcha">
                                    <input type="text" id="cap" name="cap" />
                                </div>
                            </div>
                                <div>
                                    <input type="submit" name="submit" value="submit" />
                                </div>
                            </div>

                            <input type="hidden" name="token" value="'.$token.'"></input>
                            <input type="hidden" name="captcha" value="'.$cookie.'"></input>
                        </form>
                        <div class="credits">
                            <div class="ll">
                            <p class="cred">By</p>
                            <p class="cred">TH3 M3&copy;H@N!&copy;</p>
                        </div>
                        </div>
                    </center>   
                </div>
            </body>
        </html>'

?>

1 个答案:

答案 0 :(得分:1)

很好,在您发布图像内容后,问题非常清楚。如果您将在记事本或记事本++中打开文件,您将看到它以响应中收到的卷曲的HTTP标头开头
HTTP/1.1 200 OK Server: Apache-Coyote/1.1 Pragma: no-cache Cache-Control: no-cache Expires: Thu, 01 Jan 1970 00:00:00 GMT Max-Age: Thu, 01 Jan 1970 00:00:00 GMT Content-Type: image/jpg Transfer-Encoding: chunked Date: Wed, 02 Jul 2014 16:07:35 GMT

因此,为了解决这个问题,只需将curl_setopt($ch, CURLOPT_VERBOSE, 1);更改为curl_setopt($ch, CURLOPT_VERBOSE, FALSE);

即可