curl post请求表单_CURL + POST +多部分/表单数据

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!

解决方案

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值