Robert
Robert

Reputation: 812

How can I check a external url

I have a question, is there any way to check a url if contain a word and then show this value? For example I want to check if in the external site exist ver. 1.x.x.x, this value ver. 1.x.x.x never will be the same. I make something like this, but maybe is another solution.

<form enctype="multipart/form-data" action="" method="post">
    <input name="url" type="text"/>
    <input type="submit" name="submit" value="Check" class="btn btn-primary"/><br/>
</form>


<?php
if(isset($_POST['submit'])) {
    $url = $_POST['url'];
}
$content = file_get_contents($url);
$first_step = explode( '<p class="copyright">' , $content );
$second_step = explode("</p>" , $first_step[1] );
echo $second_step[0];
?>

In the external url this is the html container

<p class="copyright">
Help Us -
<a href="http://www.externalsite.com/">
(ver. 1.x.x.x)
<br>

Upvotes: 1

Views: 311

Answers (2)

user4488093
user4488093

Reputation:

You can use SimpleHTMLDOm or DOMDocument lib in PHP. Or many other libraries :)

SimpleHtmlDom lib

$html = file_get_html($url);
echo $html->find("p[class=copyright]",0)->innertext;

DOMDocument

$doc = new DOMDocument();
$doc->loadHTMLFile($url);

$xml = simplexml_import_dom($doc);

var_dump($xml->xpath('//*[@class="copyright"]'));

Upvotes: 1

Ne Ma
Ne Ma

Reputation: 1709

Your question has two parts to it. The first is to request the page, which you can do with a simple curl request. As taken from the php docs here is an example of a curl request.

<?php 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, "example.com"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
$output = curl_exec($ch); // $output is the content
curl_close($ch);

The next part is to actually get that value. A fantastic and disgustingly upvoted stack overflow answer has already been done on this and is fairly straight forward to follow.

Upvotes: 1

Related Questions