如何使用php curl将数据发布到ASP网站 [英] How to post data to ASP site using php curl

查看:172
本文介绍了如何使用php curl将数据发布到ASP网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用php curl从我的网站发布数据到ASP网站。



ASP网站是 http://www.hotfrog.com.au/AddYourBusinessSingle.aspx



对于这个点我接近如下


  1. 使用PHP curl从网站抓取网页html源,



  2. 从该来源中抽取asp隐藏的变数值

  3. 表单字段


  4. 并使用PHP curl将数据发布到目标ASP网站网址,但响应是页面表单信息没有输入详细信息,甚至没有显示验证消息


  5. / ol>

    ASP网站需要下面的表单字段



    ctl00 $ contentSection $ CompanyDetailsControl $ txtBusinessName



    ctl00 $ contentSection $ CompanyDetailsControl $ txtSuburb



    是直接发布或在发布之前需要这些字段名称的任何编码目标网站由PHP curl



    任何人都可以有这个解决方案或任何其他方法的ASP网站由php curl?

    解决方案

    使用

      function get_headers_from_curl_response($ headerContent){

    $ headers = [];

    //拆分每个double新行上的字符串。
    $ arrRequests = explode(\r\\\
    \r\\\
    ,$ headerContent);

    //响应头的循环。 count()-1到
    //避免在esponse的主体之前的额外换行的空行。
    for($ index = 0; $ index< count($ arrRequests) - 1; $ index ++){

    foreach(explode(\r\\\
    ,$ arrRequests [$ index])as $ i => $ line){
    if($ i === 0){
    $ headers [$ index] ['http_code'] = $ line;
    }
    else {
    list($ key,$ value)= explode(':',$ line);
    $ headers [$ index] [$ key] = $ value;
    }
    }
    }

    return $ headers;
    }

    function regexExtract($ text,$ regex,$ regs,$ nthValue){
    if(preg_match($ regex,$ text,$ regs)){
    $ result = $ regs [$ nthValue];
    }
    else {
    $ result =;
    }

    return $ result;
    }

    $ regexViewstate ='/ __ VIEWSTATE\value = \(。*)\/ i';
    regexEventVal ='/ __ EVENTVALIDATION\ value = \(。*)\/ i';

    $ ch = curl_init(__ YOUR__URL__HERE__);
    curl_setopt($ ch,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ ch,CURLOPT_HEADER,1);
    curl_setopt($ ch,CURLOPT_COOKIEJAR,'cookies.txt');

    $ response = curl_exec($ ch);
    curl_close($ ch);

    $ viewstate = regexExtract($ response,$ regexViewstate,$ regs,1);
    $ eventval = regexExtract($ response,$ regexEventVal,$ regs,1);

    $ params = [
    '__EVENTTARGET'=> '',
    '__EVENTARGUMENT'=> '',
    '__VIEWSTATE'=> $ viewstate,
    '__EVENTVALIDATION'=> $ eventval,
    'ctl00%24txtUsername'=> 'xxx',
    'ctl00%24txtPassword'=> 'xxx',
    'ctl00 $ ImgLogin.x'=> '0',
    'ctl00 $ ImgLogin.y'=> '0',];

    $ ch2 = curl_init(http://gsc.klub-modul.dk/cms/ShowContentPage.aspx?ContentPageID=1);
    curl_setopt($ ch2,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ ch2,CURLOPT_HEADER,1);
    curl_setopt($ ch2,CURLOPT_POST,true);
    curl_setopt($ ch2,CURLOPT_SSL_VERIFYPEER,false);
    curl_setopt($ ch2,CURLOPT_POSTFIELDS,http_build_query($ params));
    curl_setopt($ ch2,CURLOPT_COOKIE,'cookies.txt');
    curl_setopt($ ch2,CURLOPT_COOKIEJAR,'cookies2.txt');

    $ response2 = curl_exec($ ch2);
    curl_close($ ch2);

    foreach(get_headers_from_curl_response($ response2)as $ value){
    foreach($ value as $ key => $ value2){
    echo $ key。:。 $ value2。< br />;
    }
    }


    I need to post data to ASP site using php curl from my website.

    The ASP site is http://www.hotfrog.com.au/AddYourBusinessSingle.aspx

    For this point I approached like below

    1. Crawled the webpage html source from that website using PHP curl to maintain cookies & sessions

    2. From that source asp hidden variable values extracted

    3. Prepared post string with required form fields

    4. And posted that data to target ASP site url using PHP curl but response is that page form information without entry details and even its not shows validation messages from curl response for non entry fields.

    5. For this process maintained same values for CURLOPT_USERAGENT, CURLOPT_COOKIEJAR, CURLOPT_COOKIEFILE

    The ASP site required form fields like this below

    ctl00$contentSection$CompanyDetailsControl$txtBusinessName

    ctl00$contentSection$CompanyDetailsControl$txtSuburb

    are them to be posted directly or there any encode needed for these field names before posting to target site by PHP curl

    Can anyone have solution for this solution or any other approaches for ASP sites by php curl?

    解决方案

    use

    function get_headers_from_curl_response($headerContent) {
    
        $headers = [];
    
        // Split the string on every "double" new line.
        $arrRequests = explode("\r\n\r\n", $headerContent);
    
        // Loop of response headers. The "count() -1" is to
        //avoid an empty row for the extra line break before the body of the esponse.
        for ($index = 0; $index < count($arrRequests) - 1; $index++) {
    
            foreach (explode("\r\n", $arrRequests[$index]) as $i => $line) {
                if ($i === 0) {
                    $headers[$index]['http_code'] = $line;
                }
                else {
                    list ($key, $value) = explode(': ', $line);
                    $headers[$index][$key] = $value;
                }
            }
        }
    
        return $headers;
    }
    
    function regexExtract($text, $regex, $regs, $nthValue) {
        if (preg_match($regex, $text, $regs)) {
            $result = $regs[$nthValue];
        }
        else {
            $result = "";
        }
    
        return $result;
    }
    
    $regexViewstate = '/__VIEWSTATE\" value=\"(.*)\"/i';
    regexEventVal   = '/__EVENTVALIDATION\" value=\"(.*)\"/i';
    
    $ch = curl_init("__YOUR__URL__HERE__");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt');
    
    $response = curl_exec($ch);
    curl_close($ch);
    
    $viewstate = regexExtract($response, $regexViewstate, $regs, 1);
    $eventval  = regexExtract($response, $regexEventVal, $regs, 1);
    
    $params = [
        '__EVENTTARGET'       => '',
        '__EVENTARGUMENT'     => '',
        '__VIEWSTATE'         => $viewstate,
        '__EVENTVALIDATION'   => $eventval,
        'ctl00%24txtUsername' => 'xxx',
        'ctl00%24txtPassword' => 'xxx',
        'ctl00$ImgLogin.x'    => '0',
        'ctl00$ImgLogin.y'    => '0',];
    
    $ch2 = curl_init("http://gsc.klub-modul.dk/cms/ShowContentPage.aspx?ContentPageID=1");
    curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch2, CURLOPT_HEADER, 1);
    curl_setopt($ch2, CURLOPT_POST, true);
    curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch2, CURLOPT_POSTFIELDS, http_build_query($params));
    curl_setopt($ch2, CURLOPT_COOKIE, 'cookies.txt');
    curl_setopt($ch2, CURLOPT_COOKIEJAR, 'cookies2.txt');
    
    $response2 = curl_exec($ch2);
    curl_close($ch2);
    
    foreach (get_headers_from_curl_response($response2) as $value) {
        foreach ($value as $key => $value2) {
            echo $key.": ".$value2."<br />";
        }
    }
    

    这篇关于如何使用php curl将数据发布到ASP网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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