简体   繁体   中英

Applying JavaScript after a cURL request

I am having some trouble applying JavaScript after I have used cURL to retrieve HTML data from an external website.

The idea is that it curl request makes a post to a website which returns a table of different results. For some reason I want to perform some javascript (preferably jQuery) on the results but it does not seem to work.

I believe it might be something to do with the order in which everything is loaded.

<!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>

I have tried moving the JavaScript above the end body tag and also in the the head making sure jQuery loads first but no joy, even separated the header and footer into separate files but still no joy.

The only luck I have been having is using DOM Document to remove classes from the HTML that has been copied over during the cURL request, but I am unable to apply any.

I added

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

To hope use the getElementByID() however I get the impression that it cant see it because it not part of the original HTML that was retrieved.

Any clues? Many Thanks.

you are not very clear about what want do with js, but maybe you just need execute js when document ended be loaded. Ty put js code inside document ready:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM