3
A
回答
0
你应该能够使用fopen()
为你想要的。它可以接受URL。
echo "<script type='text/javascript' src='myjavascript.js'></script>";
$handle = @fopen("http://www.example.com/", "r");
if ($handle) {
while (!feof($handle)) {
$buffer = fgets($handle, 4096);
echo $buffer;
}
fclose($handle);
}
9
如果您fopen_wrappers启用后,您可以使用file_get_contents()检索页面,然后呼应它作为输出之前插入JavaScript的内容。
$content = file_get_contents('http://example.com/page.html');
if($content !== FALSE) {
// add your JS into $content
echo $content;
}
这当然不会影响原来的页面。
0
使用CURL可能是最简单的,但我更喜欢自己做东西。这将连接到给定的地址并返回页面的内容。但它也会返回标题,因此请注意:
function do_request ($host, $path, $data, $request, $specialHeaders=null, $type="application/x-www-form-urlencoded", $protocol="", $port="80")
{
$contentlen = strlen($data);
$req = "$request $path HTTP/1.1\r\nHost: $host\r\nContent-Type: $type\r\nContent-Length: $contentlen\r\n";
if (is_array($specialHeaders))
{
foreach($specialHeaders as $header)
{
$req.=$header;
}
}
$req.="Connection: close\r\n\r\n";
if ($data != null) {
$req.=$data;
}
$fp = fsockopen($protocol.$host, $port, $errno, $errstr);
if (!$fp) {
throw new Exception($errstr);
}
fputs($fp, $req);
$buf = "";
if (!feof($fp)) {
$buf = @fgets($fp);
}
return $buf;
}
+0
是的,file_get_contents()可能也更容易,但是如果fopen_wrappers未启用,这将工作。 – Cameron 2009-07-07 19:32:45
请说明。这让您的10k读者感到困惑。 – Pacerier 2015-03-05 23:28:27