Chinmay Ratnaparkhi Chinmay Ratnaparkhi - 6 months ago 15
PHP Question

Reading source of another website with PHP and giving it to JavaScript as a string

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?

Answer

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