开发者

Posting images via CURL to a form

I've had a tough time trying to find a solution for the following problem, I need to use curl to submit a form on a website, however it also needs to upload a picture its a normal input file field

<input type="file" name="image"/>

I have a class and the curl function is defined like this

function fetch($url, $username='',  $data='', $proxy=''){


    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_URL, $url);

    curl_setopt($ch, CURLOPT_HEADER, true);


    if(isset($proxy)) {     
    curl_setopt($ch,CURLOPT_TIMEOUT,30); 
    curl_setopt($ch, CURLOPT_PROXY, $proxy); 
    curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
    curl_setopt($ch, CURLOPT_PROXYPORT, $proxy);
    curl_setopt($ch, CURLOPT_PROXYUSERPWD, 'proxyadmin:parola');

    }

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    curl_setopt($ch, CURLOPT_FRESH_CONNECT,true);

    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/3.0 (compatible; MSIE 6.0; Windows NT 5.0)"); 

    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

    curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
    if($username) {
    curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie/{$username}.txt开发者_如何学JAVA");

    curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie/{$username}.txt");
    }
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 


    if (is_array($data) && count($data)>0){

        curl_setopt($ch, CURLOPT_POST, true);

        $params = http_build_query($data);

        curl_setopt($ch, CURLOPT_POSTFIELDS, $params);

    }


    if (is_array($this->headers) && count($this->headers)>0){

        curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers);

    }



    $this->result = curl_exec($ch);
    $curl_info = curl_getinfo($ch);
    $header_size = $curl_info["header_size"];
    $this->headers = substr($this->result, 0, $header_size);
    $this->http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $this->error = curl_error($ch);

    curl_close($ch);    

}

Anyone could help me? very lost


If you change the following block of code:

// from
if (is_array($data) && count($data)>0){
    curl_setopt($ch, CURLOPT_POST, true);
    $params = http_build_query($data);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
}

// to
if (is_array($data) && count($data)>0) {
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}

And to have it post the file uploaded by the user, set up your data array like this:

// make sure $_FILES['file'] has been uploaded and is valid
$data = array('field' => 'value',
              'name' => 'test',
              'email' => 'something',
              'file' => '@' . $_FILES['file']['tmp_name']
             );

fetch($url, $username, $data);

That will tell curl to send a form post with a file upload. By setting the post fields to an array and prepending an & to the full path of a file that is a value of the array, curl will send an multipart/form-data post request with your file upload.

See curl file upload example

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜