-2

I want to read the meta-description of another website as a string in JavaScript. I tried using CORS but get an error saying "No 'Access-Control-Allow-Origin' header is present on the requested resource."

I was suggested that I could use PHP to do this. I don't know PHP and need some help. How do I call a PHP function to read source of some webpage on a different domain and then feed the output to a JavaScript function as a string?

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197

3 Answers3

0

Here's one way to do it:

  1. Set up a page source_getter.php on your server and include the following code (from this answer):

$html = file_get_contents('your_url_here'); echo $html;

  1. If you're using jQuery, run a request like this:

$.ajax({ url : 'source_getter.php', success : function (result) { doSomethingWithResult(result); // result will equal $html from your PHP code }, error : function () { alert("error"); } })

I haven't tested this code specifically but it should work just fine.

Community
  • 1
  • 1
Sander Moolin
  • 450
  • 4
  • 11
  • You're downloading (and returning) the websites entire html source code, while the question specificly asks for just the meta-description tag. It's an unnessesary use of resources as well as returning the wrong information. – icecub May 02 '16 at 17:22
0

This is probebly the easy way to do it:

<?php

// Get Meta Tags from the given URL
$tags = get_meta_tags('http://www.example.com');

?>

<!DOCTYPE HTML>
<html>
    <head>
        <script type="text/javascript">

        var urlMetaDesc = "<?php echo $tags['description']; ?>";

        alert(urlMetaDesc);

        </script>
    </head>
    <body>
    </body>
</html>

Keep in mind though that if a website didn't set the meta-description tag, nothing will be returned and no alert will be shown.

icecub
  • 8,615
  • 6
  • 41
  • 70
  • Could you Test your Code? just Replace your url with ***http://www.yahoo.com*** What did you get? Then try to view the source-code. Are both your Result and What you see in the Source-Code consistent with each other? – Poiz May 02 '16 at 17:42
  • This worked well for me, thank you so much! Is it possible to let JavaScript feed the URL to PHP though? I have a list of URLs I want to get the description for, so it would be great if I could just loop through an array of URLs. – Chinmay Ratnaparkhi May 02 '16 at 18:30
  • @ChinmayRatnaparkhi Yes and no. You see, PHP does its work BEFORE the website is loaded. Javascript does its work DURING or AFTER the website is loaded. So if you really want Javascript to send the URL towards the PHP script, the website must reload in order for PHP to be able to do something with it. You could use Ajax to prevent a visible website reload. But the script will become a whole lot more complicated for that to work. – icecub May 02 '16 at 18:38
0

Here is a simple, straight-forward way to help you get what you want. Try it out first... Paste the entire code below on an Empty PHP File and run it. No real need for Ajax in this Simple Scenario. So you have 2 Options:

OPTION NR. 1

<?php
    //SIMPLY CHANGE THE URL TO THE URL YOU DESIRE
    $siteURL        = "https://yahoo.com/";
    $siteContent    = file_get_contents($siteURL);
    $metaRx         = "#<meta .*description.*>$#m";
    preg_match($metaRx, $siteContent, $metaMatches);
    $metaString     = str_replace("'", "\'", $metaMatches[0]);

    //DUMP THE ARRAY OF MATCHES TO THE SCREEN... JUST TO EXPLORE THE RESULTS
    var_dump($metaMatches);
?>
<script type="text/javascript">
    //EXPOSE THE META TO YOUR JAVASCRIPT USING A GLOBAL VARIABLE (FOR EXAMPLE).
    var SITE_META_DESC = '<?php echo $metaString; ?>';
    // DUMP VALUE TO THE SCREEN USING ALERT....
    alert(SITE_META_DESC);
</script>

Here is another alternative... it is concise and simple; however it may not give you the result you want:

OPTION NR. 2

<?php
    //SIMPLY CHANGE THE URL TO THE URL YOU DESIRE 
    $metaTags           = get_meta_tags('https://yahoo.com/');
    $metaDescription    = $metaTags["description"];
    var_dump($metaDescription);

    //USING A DATA-SOURCE ARRAY:
    $arrURLs            = array("http://sbb.ch", "http://alibabaexpress.com", "https://yahoo.com", "http://badoo.com" );
    $arrMetaDescs       = array();

    // LOOP THROUGH THE $arrURLs AND GET THE META
    // AND STORE THE RESULT IN AN ARRAY TOO.
    foreach($arrURLs as $url){
        //IF YOU WANT YOU COULD USE THE URL AS KEY FOR EASIER IDENTIFICATION
        try{
            $metaTags                   = get_meta_tags($url);
            if($metaTags){
                $key                    = preg_replace("&(https:\/\/|http:\/\/|www\.|\/.*$)?&", "", $url);
                $arrMetaDescs[$key]     = $metaTags["description"];
            }
        }catch(Exception $e){

        }
    }
    var_dump($arrMetaDescs);

?>
<script type="text/javascript">
    //EXPOSE THE META TO YOUR JAVASCRIPT USING A GLOBAL VARIABLE (FOR EXAMPLE).
    var SITE_META_DESC = '<?php echo $metaDescription; ?>';
    alert(SITE_META_DESC);


    // IN THE CASE OF ARRAY-BASED META-EXTRACTION,
    // STORE THE META VALUES IN JSON FORMAT FOR JAVASCRIPT
    var ARR_META_DESC_EXTRACT = '<?php echo json_encode($arrMetaDescs); ?>';
    console.log(ARR_META_DESC_EXTRACT);
</script>
Poiz
  • 7,611
  • 2
  • 15
  • 17
  • Why not simply use `get_meta_tags()` instead of downloading the entire source code and making the server cpu work with regular expressions? – icecub May 02 '16 at 17:24
  • ***get_meta_tags*** is really the perfect solution here. However, some people complain that this function does not work for them... I will actually update the answer... Thanks for the reminder, though.... ;-) – Poiz May 02 '16 at 17:29
  • The reason it doesn't work for some people is because a lot of websites these days no longer use the meta-description tag. Google is an example of that. `get_meta_tags()` however does exactly what it's supposed to do. And you're welcome :) – icecub May 02 '16 at 17:34
  • Sure... You are right.... But check this out yourself... Try to run the Code above yourself.... The first alternative returned/alerted exactly what @Chinmay Ratnaparkhi wanted. The second (preferred & concise) alternative returned & alerted only ***My Yahoo*** Would you (perchance) know why? – Poiz May 02 '16 at 17:46
  • I honestly don't know why this is happening. Especially since "My yahoo" doesn't appear in any of the source code at all. Very strange. – icecub May 02 '16 at 18:15
  • Me neither... It's a little funny, right... but take amazon for example: ***https://amazon.com*** , it works perfectly.... Well, we let Mr. Chinmay Ratnaparkhi decide which way to go... – Poiz May 02 '16 at 18:25
  • Your Option2 works well for me, thank you so much! Although, instead of hardcoding the URL, is it possible to loop through an array of URLs and get meta descriptions? – Chinmay Ratnaparkhi May 02 '16 at 18:32
  • ** :-) ** You may just mark this as complete and as the correct answer. It may help others as well... – Poiz May 02 '16 at 18:34
  • Yes, it is possible to loop through and array of URLs and get the meta descriptions... See again the modified version using option 2. – Poiz May 04 '16 at 06:02