CURL + POST + multipart / form-data

I am trying to clear a website using the PHP, CURL and POST method to submit the form before the web clears the page. The problem I'm experiencing is that it is related to the POST method: the data is not transmitted to the server, so the cleared web page does not contain what I'm looking for.

I am sure the problem is with the type of the form: enctype = "multipart / form-data". How can I manage this POST request, given that the form is multipart / form-data? Should I encode post_string in a special way?

Here is the code I'm using:

 function curl($url) {

//POST string
$post_string="XXXX";

$options = Array(
        CURLOPT_RETURNTRANSFER => TRUE,  
        CURLOPT_FOLLOWLOCATION => TRUE, 
        CURLOPT_AUTOREFERER => TRUE, 
        CURLOPT_CONNECTTIMEOUT => 120,  
        CURLOPT_TIMEOUT => 120, 
        CURLOPT_MAXREDIRS => 10, 
        CURLOPT_USERAGENT => "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1a2pre) Gecko/2008073000 Shredder/3.0a2pre ThunderBrowse/3.2.1.8",  
        CURLOPT_URL => $url, 
        CURLOPT_CAINFO => dirname(__FILE__)."/cacert.pem",

        CURLOPT_POSTFIELDS => $post_string,

    );

    $ch = curl_init(); 
    curl_setopt_array($ch, $options);   
    $data = curl_exec($ch); 
    curl_error($ch);
    curl_close($ch);       
    return $data;   
}

$scraped_page = curl("XXXURLXXX");    
echo $scraped_page; 

Thank!

+3
source share
2 answers

Set CURLOPT_POST to true:

CURLOPT_POST = true

Then fill in the message fields, like this 'setup':

$postfields = array();
$postfields['field1'] = 'value1';
$postfields['field2'] = 'value2';
CURLOPT_POSTFIELDS => $postfields

, Content-Type multipart/form-data.

PHP

+4

, $post_string .

CURLOPT_POST true.

+1

All Articles