【发布时间】: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