开发者

Load a text/javascript into a PHP page

I'm trying to load a javascript file into a PHP page and then i can use jquery load to bring that data into my website.

http://www.themoviequotes.com/widgets/javascript?n=1&l=1&g=3

This is the URL. If you put this into a browser it will display document.write etc and then the actual content i want.

What PHP function could i use to get the data, i've tried file_get_contents but it returns all 开发者_如何学JAVAof the html/javascript tags etc...like this:

document.write("
Finding Forrester (2000)

William Forrester: Writers write things to give readers something to read.
"); 


<?php
   $quote = file_get_contents('http://www.themoviequotes.com/widgets/javascript?n=1&l=1&g=3');
   $quote_text = array();
   preg_match('!document\.write\(\"(.+)\"\);!', $quote, $quote_text);
   $quote_text = stripslashes($quote_text[1]);
   echo($quote_text);

If you want to capture the value in javascript, you also could override the document.write() function to instead be a callback function to insert the quote somewhere in your dom.


Put a script tag where you want the content to go, and have the src attribute set to that address.

<script type="text/javascript" src="http://www.themoviequotes.com/widgets/javascript?n=1&l=1&g=3"></script>


You mean you want to execute JavaScript inside PHP? You'll need a parser/JavaScript engine for that. It's not that Javascript magically works; the browser contains the logic for that and executes the Javascript code, but I don't know if there are any Javascript libraries for that.

In this case you could get away by using some string functions or regular expressions to strip all html and javascript from this code and keep the title and text.


try this:

$ch = curl_init("http://www.themoviequotes.com/widgets/javascript?n=1&l=1&g=3");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);      
curl_close($ch);
echo $output;

As the above URL returns you HTML data, there is no way to parse. You need to use substring function to get the desired result. or you can use the preg_match method to extract the data or explode function also.

hope this is helpful.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜