Hello
I check tow way, Curl and file_get_contents. Curl is hard and file_get_contents is normal.
First way Curl: article linke : http://www.merchantos.com/makebeta/p...inks-with-php/
This way have a problem:
Error, insert query failed
<?php
//connect to database
$hostname = 'localhost';
$dbname = 'newdb';
$username = 'root';
$password = '';
// Let's connect to host
mysql_connect($hostname, $username, $password) or DIE('Connection to host is failed, perhaps the service is down!');
// Select the database
mysql_select_db($dbname) or DIE('Database name is not available!');
//get post value
$titel=$_POST['titel'];
$ural=$_POST['ural'];
$page_linkus=$_POST['page_linkus'];
$exposition=$_POST['exposition'];
//------------------------
function storeLink($titel,$ural,$ural,$ural) {
$query = "INSERT INTO links (titel, ural, page_linkus, exposition,) VALUES ('$titel', '$ural' ,'$page_linkus', '$exposition')";
mysql_query($query) or die('Error, insert query failed');
}
$target_url = "http://www.merchantos.com/";
$userAgent = 'Googlebot/2.1 (http://www.googlebot.com/bot.html)';
// make the cURL request to $target_url
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$html= curl_exec($ch);
if (!$html) {
echo "<br />cURL error number:" .curl_errno($ch);
echo "<br />cURL error:" . curl_error($ch);
exit;
}
// parse the html into a DOMDocument
$dom = new DOMDocument();
@$dom->loadHTML($html);
// grab all the on the page
$xpath = new DOMXPath($dom);
$hrefs = $xpath->evaluate("/html/body//a");
for ($i = 0; $i < $hrefs->length; $i++) {
$href = $hrefs->item($i);
$url = $href->getAttribute('href');
storeLink($titel,$ural,$ural,$ural);
echo "<br />Link stored: $url";
}
?>
Second way: use of file_get_contents. this way write myself:
Error:
Error, insert query failed
Warning: file_get_contents(www.com.com) [function.file-get-contents]: failed to open stream: No such file or directory in D:\xampp\htdocs\ est.php on line 20
The string ‘http://www.sloo.com’ was not found in the string ‘’
<?php
//connect to database
$hostname = 'localhost'; // Your MySQL hostname. Usualy named as 'localhost', so you're NOT necessary to change this even this script has already online on the internet.
$dbname = 'newdb'; // Your database name.
$username = 'root'; // Your database username.
$password = ''; // Your database password. If your database has no password, leave it empty.
// Let's connect to host
mysql_connect($hostname, $username, $password) or DIE('Connection to host is failed, perhaps the service is down!');
// Select the database
mysql_select_db($dbname) or DIE('Database name is not available!');
//get post value
$titel=$_POST['titel'];
$ural=$_POST['ural'];
$page_linkus=$_POST['page_linkus'];
$exposition=$_POST['exposition'];
$query = "INSERT INTO links (titel, ural, page_linkus, exposition,) VALUES ('$titel', '$ural' ,'$page_linkus', '$exposition')";
mysql_query($query) or die('Error, insert query failed');
//get contents $ural (.e.g $ural=www.hello.com)
$get = file_get_contents($ural);
//find $find to $get
$find = 'http://www.sloo.com';
$pos = strpos($get, $find);
if ($pos === false) {
echo "The string '$find' was not found in the string '$get'";
} else {
echo "The string '$find' was found in the string '$get'";
echo " and exists at position $pos";
}
?>
Pelse help me for obviation error!?
Which way is best?