CURL + POST +多部分/表单数据 [英] CURL + POST + multipart/form-data

查看:245
本文介绍了CURL + POST +多部分/表单数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用PHP,CURL和POST方法来抓取网站,以便在网站抓取页面之前提交表单.我遇到的问题是与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.

我退出,确定问题与表单类型有关:enctype ="multipart/form-data". 考虑到表单是multipart/form-data,我如何管理此POST请求? 我必须以特殊方式对post_string进行编码吗?

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; 

谢谢!

推荐答案

将CURLOPT_POST设置为true:

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


如果value是一个数组,则Content-Type标头将设置为multipart/form-data.

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

PHP手册

这篇关于CURL + POST +多部分/表单数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆