【问题标题】:Applying JavaScript after a cURL request在 cURL 请求后应用 JavaScript
【发布时间】:2016-05-31 12:58:14
【问题描述】:

在使用 cURL 从外部网站检索 HTML 数据后,我在应用 JavaScript 时遇到了一些问题。

这个想法是它 curl 请求向一个网站发布一个帖子,该网站返回一个不同结果的表格。出于某种原因,我想对结果执行一些 javascript(最好是 jQuery),但它似乎不起作用。

我相信这可能与加载所有内容的顺序有关。

<!DOCTYPE html>
<html>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title></title>
<link rel="stylesheet" type="text/css" href="css/style.css">
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/r/bs-3.3.5/jq-2.1.4,dt-1.10.8/datatables.min.css"/>
<script type="text/javascript" src="https://cdn.datatables.net/r/bs-3.3.5/jqc-1.11.3,dt-1.10.8/datatables.min.js"></script>
</head>
<body>
<div class="container">
<div class="table-responsive">
<?php

$search_term = $_POST["search_term"];

$regex = "/^(\d[\s-]?)?[\(\[\s-]{0,2}?\d{3}[\)\]\s-]{0,2}?\d{3}[\s-]?\d{4}$/i";

if (preg_match($regex, $search_term)) {

    $post_data['number'] = $search_term;
    $curl_connection = curl_init('http://www.xxxxxxxxxxxxxxxxxxx.com/numbersearch.php');

} else {
    $post_data['search_name'] = $search_term;
    $curl_connection = curl_init('http://www.xxxxxxxxxxxxxxxxxxx.com/companysearch.php');
}

foreach ( $post_data as $key => $value) {
    $post_items[] = $key . '=' . $value;
}

$post_string = implode ('&', $post_items);

//set options
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
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);

//set data to be posted
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);

//perform our request
$result = curl_exec($curl_connection);

$dom = new DOMDocument();
@$dom->loadHTML($result);
$xpath = new DOMXPath($dom);

$domTables = $xpath->query('.//div[@class="boardcontainer"]');
$tbclass = $dom->getElementsByTagName('table');
$tdtag = $dom->getElementsByTagName('td');

//from the main database table only.
$table = $domTables->item(0);

//main loop throuth the table we pull from its source
foreach($table->childNodes as $child){ 


    //looks at the table tag and removes css from source
    foreach($tbclass as $tbclasses){
        $tbclasses->removeAttribute('cellpadding'); 
        $tbclasses->removeAttribute('cellspacing'); 
        $tbclasses->removeAttribute('border'); 
        $tbclasses->removeAttribute('width');

        //adds bootstrap css class for table 
        $tbclasses->setAttribute('class', 'table table-bordered');      
        $tbclasses->setAttribute('id', 'example');
    }

    //looks at the td tag within table and removes css from source
    foreach ($tdtag as $tdtags) {
        $tdtags->removeAttribute('class');      
        $tdtags->removeAttribute('width');
        $tdtags->removeAttribute('align');
        $tdtags->removeAttribute('bgcolor');
        $tdtags->removeAttribute('colspan');     
        $tdtags->removeAttribute('height');     
    }
    //outputs the html for display
    echo $child->ownerDocument->saveHTML($child);   
}

/*
This used to work until I added the Dom Document part to it.
*/

if (empty($table)) {
        echo "Nothing found";
    }

//close the connection
curl_close($curl_connection);
?>
</div>
</div>
<script type="text/javascript" charset="utf-8">
    $(document).ready(function() {
        $("#example").DataTable();
    } );
</script>
</body>
</html>

我尝试将 JavaScript 移动到结束 body 标记的上方以及头部,确保 jQuery 首先加载但没有乐趣,甚至将页眉和页脚分成单独的文件但仍然没有乐趣。

我唯一的运气是使用 DOM 文档从 HTML 中删除在 cURL 请求期间复制的类,但我无法应用任何类。

我加了

$tbclasses->setAttribute('id', 'example');

希望使用 getElementByID() 但是我觉得它看不到它,因为它不是检索到的原始 HTML 的一部分。

有什么线索吗? 非常感谢。

【问题讨论】:

  • javascript 代码在哪里? html 是什么样的,您需要在该 html 中进行哪些更改?这整个问题不是很清楚。请注意,无论您在 php 中做什么,都需要先输出,然后 javascript 才能在浏览器中对其进行任何操作
  • 您是否尝试使用您的 JS 代码生成完整的 html 文档并将其返回给浏览器?
  • @charlietfl 我已经添加了一些示例 JS,我相信也已经输出了它。这是“echo $child->ownerDocument->saveHTML($child);”吗?还不够?
  • @messerbill 听起来像是您建议我继续使用 Dom Document 以这种方式添加 JS 而不是事后添加?
  • cURL 在服务器上运行,javascript 在前端运行。所有 javascript 在 cURL 之后运行

标签: javascript php jquery html curl


【解决方案1】:

你不是很清楚要用 js 做什么,但也许你只需要在加载文档结束时执行 js。 Ty 将 js 代码放入文档中:

$( document ).ready(function() {
   //your code
});

【讨论】:

  • 现在我只是想从 (datatables.net)
  • 我的想法是,我尝试应用什么 JavaScript 代码并不重要,重要的是为什么在这个过程中任何 JavaScript 代码都不能工作。
  • 你确定curl返回的html是正确的?数据表与 html sintax 有点奇怪。注意 php 生成的 html 代码,并且只有在 js 代码运行之后。所以几乎可以肯定 html 有问题。
  • 我在这两点上都同意你的观点,正如我在 DataTables 中注意到的那样,它们需要“thead”和“th”在表格 html 中,我将不得不弄清楚如何注入再次使用 DomDocument。然而,它并没有解决 PHP 似乎在渲染 HTML 的问题,尽管我唯一的证据是它本身的浏览器,查看源代码显示了它应该的代码,除了 JavaScript 应该进行的更改。很奇怪。
  • cont: 你认为我在这里缺少什么使用 cURL 和 $DOM 从我必须承认的源生成 HTML 非常混乱,但我相信我已经使用 DomDocument 和DOMX 路径。我想我可能必须创建一个布局相似但 html 更清晰的虚拟站点,看看它是否可以确定它是否是 PHP 输出的 HTML 问题。
猜你喜欢
  • 2012-01-17
  • 2021-08-02
  • 2014-10-20
  • 1970-01-01
  • 1970-01-01
  • 2019-07-04
  • 1970-01-01
  • 2023-04-07
  • 2021-03-17
相关资源
最近更新 更多