【问题标题】:PHP curl doesn't work on page that contain Ajax ScriptPHP curl 在包含 Ajax 脚本的页面上不起作用
【发布时间】:2015-11-25 09:35:18
【问题描述】:

网页中有一个带有两个过滤器的表格:
1.期间
2. 细分
当用户提交表单时,数据将显示在表格中。

我需要使用 curl 获取数据。但我有一个问题。当我尝试查看源代码时,我认为数据是用 Ajax 处理的。这是代码(来自查看源代码):

<script type="text/javascript">
    function addRow( obj1, lvl, kws, wtl, kndt, cmdf, csss ) {
        $.ajaxSetup ({  
            cache: false  
        });

          $.ajax({
              url : "wbs/api_pagename.php?lvl=" + ( parseInt( lvl ) + 1 ) + "&kws=" + kws + "&wtl=" + wtl + "&dtl=" + kndt + "&periode=" + g_periode + "&segmen=" + g_segmen,
              dataType: 'json',
              beforeSend:function(){
                  progshow();
              },
              success:function( data, textStatus, jqXHR ){
                  var tbl = document.getElementById('tbl_1');
                  temprow = document.getElementById( obj1 ).rowIndex;
                  lvl++;

                  for ( var i in data ){
                      temprow++;
                      var mainRow = tbl.insertRow( temprow );
                      var trId = obj1 + "_" + i;
                      mainRow.id = trId;

                      if (csss.length > 0) {
                          csss += ' ';
                      }

                      csss += "css_" + obj1;
                      mainRow.className = csss;

                      var newCell = mainRow.insertCell( 0 );
                      newCell.innerHTML = '';

                      var newCell = mainRow.insertCell( 1 );
                      newCell.innerHTML = '';

                      for ( _i = 1; _i < lvl - 1; _i++ ) {
                          newCell.innerHTML += '&nbsp;&nbsp;&nbsp;&nbsp;';
                      }

                      if (lvl <= 2) {
                          newCell.innerHTML += "<a href=\"javascript:doMenu('" + trId + "','" + lvl + "','" + data[i][0] + "','" + data[i][1] + "','" + data[i][2] + "','','" + csss + "');\" id='a" + trId + "'>[+]";
                      }

                      newCell.innerHTML += data[i][ 2 + parseInt( lvl ) ];
                      addrow1( mainRow, data, i, 1 );
                  }

                  proghide();
              },
              error : function( jqXHR, textStatus, errorThrown ){
                  alert(jqXHR.status);
                  alert(textStatus);
                  alert(errorThrown);
              }
          });
    }
</script>

我尝试使用 PHP curl 获取数据,但失败了。
这是我的 PHP 代码:

<?php
    $kipas1 = "entered_user=XXXXX&entered_password=XYZXYZ&login=login&redirect_to=wp-admin/";
    $cr = curl_init();

    curl_setopt( $cr, CURLOPT_URL, "http://webpage.com/index.php" );
    curl_setopt( $cr, CURLOPT_CONNECTTIMEOUT, 3000 );
    curl_setopt( $cr, CURLOPT_USERAGENT, "Mozilla/7.0 (compatible; MSIE 9.0; Windows NT 5.1)" );
    curl_setopt( $cr, CURLOPT_RETURNTRANSFER, true );
    curl_setopt( $cr, CURLOPT_SSL_VERIFYPEER, false );
    curl_setopt( $cr, CURLOPT_FOLLOWLOCATION, true );
    curl_setopt( $cr, CURLOPT_POST, true );
    curl_setopt( $cr, CURLOPT_POSTFIELDS, $kipas1 );
    curl_setopt( $cr, CURLOPT_COOKIESESSION, true );
    curl_setopt( $cr, CURLOPT_COOKIEJAR, 'C:\xampp\htdocs\coc\cookie.txt' );

    $whoCares = curl_exec( $cr ); 

    $myvars = "lvl=2&kws=ALL&wtl=ALL&dtl=ALL&periode=201511&segmen=ALL";
    curl_setopt( $cr, CURLOPT_URL, "http://webpage.com/target.php" );
    curl_setopt( $cr, CURLOPT_POST, 1 );
    curl_setopt( $cr, CURLOPT_POSTFIELDS, $myvars );
    curl_setopt( $cr, CURLOPT_FOLLOWLOCATION, 1 );
    curl_setopt( $cr, CURLOPT_HEADER, 0 );
    curl_setopt( $cr, CURLOPT_RETURNTRANSFER, 1 );

    $result = curl_exec( $cr );

    echo $result;
?>

结果仅显示选择了我的变量的选择元素(下拉列表),但表格未显示。我不知道为什么。也许是因为ajax什么的。

请帮忙。谢谢。

【问题讨论】:

  • 你必须像 ajax 从那里获取数据一样为wbs/api_pagename.php 卷曲。
  • 检查错误error_reporting(E_ALL);
  • 好的。我会尝试。谢谢

标签: javascript php ajax curl


【解决方案1】:

代码显示正在对 wbs/api_pagename.php 进行 AJAX 调用(带有多个查询字符串参数)。

如果您想通过 cURL 访问数据,您的脚本将需要对该脚本执行 HTTP GET 请求。从您发布的来源来看,此脚本将返回 JSON 数据。

【讨论】:

    猜你喜欢
    • 2016-01-06
    • 1970-01-01
    • 2015-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-04
    • 2013-07-08
    • 1970-01-01
    相关资源
    最近更新 更多