CURL + POST + multipart/form-data

走远了吗. 提交于 2020-06-22 12:37:30

问题


I am trying to scrape a website using PHP, CURL and POST method in order to submit a form before web scraping the page. The problem I am experiencing is that there is connected with POST method: no data is submitted to the server, so the scraped webpage doesn't contain what I am looking for.

I quit sure the problem is connected with the form type: enctype="multipart/form-data". How can I manage this POST request, considering that the form is multipart/form-data? Do I have to encode the post_string in a special way?

Here's 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 you!


回答1:


Set the CURLOPT_POST to true:

CURLOPT_POST = true

Then fill your post fields like this 'setup':

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

If value is an array, the Content-Type header will be set to multipart/form-data.

The PHP manual




回答2:


Yes, $post_string needs to be an array.

Also set CURLOPT_POST to true.



来源:https://stackoverflow.com/questions/22037448/curl-post-multipart-form-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!