関数に文字列を渡したいのですが、その文字列がURLに追加されます。次に、その URL に移動し、ページをサーバーに返すので、JS で操作できます。
どんなアイデアでも大歓迎です。
乾杯。
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;
}
もちろん、これは元のページには影響しません。
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);
}
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;
}