【问题标题】:PHP Curl failed to post dataPHP Curl 无法发布数据
【发布时间】:2018-10-22 12:30:58
【问题描述】:

我正在使用 PHP 编写一个 aubook 预约脚本。有一个带有可供预订日期的日历。

我成功进行了日志记录,我成功获取了随机日期,我成功获取了可用的日期参数,最后我未能发布数据并预约。

在我用这个简单的脚本成功预订后,我必须提出一个条件——如果有可用的日期,请尝试预订,否则继续刷新

    <?php

        set_time_limit(0);// to infinity for 


        $ch = curl_init();
        $headers[] = "Accept: */*";
        $headers[] = "Connection: Keep-Alive";


        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_URL, 'https://example.com/login/login.php');
        curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
        curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);


        $co = curl_exec($ch);


    $doc = new DOMDocument();
    libxml_use_internal_errors(true);
    $doc->loadHTML($co);
    # Parse the HTML
    # The @ before the method call suppresses any warnings that
    # loadHTML might throw because of invalid HTML in the page.
    $xpath = new DOMXPath($doc);

    $val1 = $xpath->query('//input[@name="_sid"]/@value')->item(0)->nodeValue;


    echo $val1; 



     echo '<br/>';

         $field['process'] = 'login';
        $field['_sid'] = $val1;
        $field['email'] = 'myemail@example.com';
        $field['pwd'] = '123456';

        $datafield = http_build_query($field);

        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
        curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $datafield);
        curl_setopt($ch, CURLOPT_VERBOSE, true);

        curl_exec($ch);

        curl_setopt($ch, CURLOPT_URL, 'https://example.com/login/myapp.php?fg_id=5568094');

        $cur = curl_exec($ch);



        $do = new DOMDocument();   // New  dom Doc to Get URL from calender of avaliable dates
    libxml_use_internal_errors(true);
    $do->loadHTML($cur);
    # Parse the HTML
    # The @ before the method call suppresses any warnings that
    # loadHTML might throw because of invalid HTML in the page.
    $xpath = new DOMXPath($do);
    $onClickAttrNodeList = $xpath->query('//a[@class="dispo"]/@onclick'); //array contains URL 

    $array = array();         // CONVERT NODE LIST  OBJECT TO ARRAY
    foreach($onClickAttrNodeList as $node){
        $array[] = $node;
    }


    $x=array();
       foreach($array as $node) {
           for($i = 0; $i < 10; ++$i) {
         $x[] = $node->nodeValue;  //PARSE ALL LINK AS TABLE
    }

    }
    $randlink = array_rand($x, 10);   //get gandom link from calender of avaliable dates 
    $link = $x[$randlink[0]];



    echo '<br/>';
    preg_match_all('#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#', $link, $match);  //Get URL from the last array
       echo "<pre>";
       $url = $match[0];
    print_r($url[0]); 

    echo'<br/>';

    parse_str( parse_url( $url[0], PHP_URL_QUERY), $arrayurl );   // GET parametres from the URL of avaliable dates to book

    var_dump($arrayurl);

 /* in this part of code
    i am trying  to post 
    parametres to book
    an appottment i failed on this step */
        $fieldbook['timestamp'] = $arrayurl[0];                      
        $fieldbook['skey'] = $arrayurl[1];
        $fieldbook['process'] = $arrayurl[2];
        $fieldbook['what'] = $arrayurl[3];
        $fieldbook['fg_id'] = $arrayurl[4];
        $fieldbook['result'] = $arrayurl[5];
        $fieldbook['issuer_view'] = $arrayurl[6];


        $datafieldbook = http_build_query($fieldbook);

        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_URL, 'https://example.com/login/action.php');
        curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
        curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $datafieldbook);
        curl_setopt($ch, CURLOPT_VERBOSE, true);

        curl_exec($ch);

        curl_setopt($ch, CURLOPT_URL, 'https://example.com/login/myapp.php?fg_id=5568094');

        $book = curl_exec($ch);



    echo'<br/>';
        echo $book;
            curl_close($ch);


    ?>

谢谢。

【问题讨论】:

  • 我不确定你的问题是什么,问题在哪里,以及你期望发生什么。
  • 您不应关闭证书验证。有可用的免费证书,没有人必须关闭安全性。
  • Adam 请阅读代码不要随机投票 /* 在这部分代码中我试图发布参数来预订我在这一步失败的预约 */

标签: php curl post


【解决方案1】:

问题解决了,我刚刚添加了用户代理和一些标题。

谢谢你们 :-)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-28
    • 2023-03-09
    • 1970-01-01
    • 2014-10-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多