CURL返回包含错误的页面(JS,CSS等错误)

时间:2018-03-23 15:47:09

标签: php curl iframe pentaho

实际上我在Stack Overflow中看到了一些类似的情况,但似乎我的PHP代码中有错误,无法正确显示页面。我试图获得的页面是来自Pentaho BI的资源。 (版本7.1.0.0.12)。我尝试了很多很多东西,但没有任何作用。

为了获取Cookie,我通过CURL PHP执行HTTP POST。这很好用,我可以从Pentaho获得Cookie。 请检查以下代码;

    $post_data['username'] = "suzy";
    $post_data['password'] = "password";
    foreach ($post_data as $key => $value) {
      $post_items[] = $key . '=' . $value;
    }
    $post_string = implode('&', $post_items);
    $curl_connection = curl_init('http://10.10.10.215:8080/pentaho/j_spring_security_check');
    curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 3000 * 10);
    curl_setopt($curl_connection, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
    curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($curl_connection, CURLOPT_POSTFIELDS, 'j_username=suzy&j_password=password');
    curl_setopt($curl_connection, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
    $result = curl_exec($curl_connection);
    $sessionID = explode("=", $url);
    $cookie = $sessionID[1];

因此,变量$ cookie包含我应该用来从Pentaho访问资源的SESSIONID。

然后我通过CURL PHP执行HTTP GET,以便从Pentaho获取页面(资源)。 这是不起作用的部分。实际上我能看到的是PHP已连接"通过我之前请求的URL和Cookie来Pentaho,并且调用返回整个页面,但是当我在浏览器中显示页面时,它会抛出很多错误,如前所述(JS,CSS错误等)。 请检查以下代码;

    $url = "http://10.10.10.215:8080/pentaho/api/repos/%3Apublic%3ASteel%20Wheels%3ADashboards%3ACTools_dashboard.wcdf/generatedContent";
    $ch = curl_init();
    $headers[] = 'Accept: text/html, application/xml;q=0.9, application/xhtml+xml, image/png, image/jpeg, image/gif, image/x-xbitmap';
    $headers[] = 'Connection: Keep-Alive';
    $headers[] = 'Cache-Control: no-cache';
    $headers[] = 'Pragma: no-cache';
    $headers[] = 'Transfer-Encoding: chunked';
    $headers[] = 'Accept-Language: nl-NL,nl;q=0.8,en_US;q=0.6,en;q=0.4';
    $headers[] = 'Accept-Encoding: gzip, deflate';
    $headers[] = 'Accept: text/plain,* /*;q=0.01';
    $headers[] = 'Content-Type: text/html; charset=utf-8';
    $headers[] = 'Cookie: JSESSIONID='.$sessionID[1];
    $user_agent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)';
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
    curl_setopt($ch, CURLOPT_POST, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, '');
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
    curl_setopt($ch, CURLOPT_ENCODING, '');
    curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    $response = curl_exec($ch); 
    echo $response;die();

我想澄清一下,我能够获得该页面,但我提到了所有这些错误。 我也尝试将这些内容添加到IFRAME中,但无法做到。有什么办法吗? 欢迎您提供所有可添加的信息!如果你有一些我可以检查的代码,还有更多。

0 个答案:

没有答案