SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    Non-Member
    Join Date
    Nov 2010
    Posts
    220
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    problem to reciprocal link checker (Curl & file_get_contents)

    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 Code:
    <?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($chCURLOPT_USERAGENT$userAgent);  
    curl_setopt($chCURLOPT_URL,$target_url);  
    curl_setopt($chCURLOPT_FAILONERRORtrue);  
    curl_setopt($chCURLOPT_FOLLOWLOCATIONtrue);  
    curl_setopt($chCURLOPT_AUTOREFERERtrue);  
    curl_setopt($chCURLOPT_RETURNTRANSFER,true);  
    curl_setopt($chCURLOPT_TIMEOUT10);  
    $htmlcurl_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\test.php on line 20
    The string 'http://www.sloo.com' was not found in the string ''
    PHP Code:
    <?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?

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,192
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Your MySQL query has an error in it, the error message your script was giving you was telling you the source of the problem.

    Note the error in quotes ","

    Code PHP:
    $query = "INSERT INTO links (titel, ural, page_linkus, exposition",") VALUES ('$titel', '$ural' ,'$page_linkus', '$exposition')";

    Your code should be

    Code PHP:
    $query = "INSERT INTO links (titel, ural, page_linkus,  exposition) VALUES ('$titel', '$ural' ,'$page_linkus',  '$exposition')";

  3. #3
    Non-Member
    Join Date
    Nov 2010
    Posts
    220
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok, thank
    Why the function preg_match Slash does not accept?
    Example:
    does not accept: www.zigweb.ir/design
    accept: www.zigweb.ir\/design
    what to do?

    function preg_match () is best or stripos ()?

  4. #4
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,192
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    If you need to match a string to a specific value then preg_match is the way to go, the reason why using www.zigweb.ir/design doesn't work is because regular expressions sees that as been a standard expression rule where as \/ is saying that the forward slash is not a rule but part of the string

  5. #5
    Non-Member
    Join Date
    Nov 2010
    Posts
    220
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank
    What can I do to work preg_match with slash?
    preg_match is best or stripos ?

  6. #6
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,192
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    If you need to check a URL for instance you can use str_replace to change the normal / to \/ like this

    PHP Code:
    $string str_replace('/''\/'$url); 

  7. #7
    Non-Member
    Join Date
    Nov 2010
    Posts
    220
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank
    How to convert the code below to Ajax or jQuery?
    Ajax or jQuery as the existence of links be investigated. => reciprocal link checker Ajax or jQuery validation
    PHP Code:
            $string str_replace('/''\/'$ural);
            
    $link "/$string/si";
            if(
    preg_match($link$contents)) {
                echo 
    "yes";
            } else
            echo 
    "no";
            } 

  8. #8
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,192
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •