无法通过CURL执行php脚本
我有一个显示数据库数据的PHP脚本 我还有另一个脚本B,它获取脚本A的内容。但我需要在脚本A执行后获取内容。但这并非偶然 //脚本A无法通过CURL执行php脚本,php,html,Php,Html,我有一个显示数据库数据的PHP脚本 我还有另一个脚本B,它获取脚本A的内容。但我需要在脚本A执行后获取内容。但这并非偶然 //脚本A enter code here <?php session_start(); if(!isset($_SESSION['username'])) { print "You have not logged in"; header('Location: index.htm');
enter code here
<?php
session_start();
if(!isset($_SESSION['username']))
{
print "You have not logged in";
header('Location: index.htm');
}
session_write_close();
?>
<?xml version="1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="cs" lang="cs">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta http-equiv
="content-language" content="cs" />
<meta name="robots" content="all,follow" />
<meta name="author" content="All: ... [Nazev webu - www.url.cz]; e-mail: info@url.cz" />
<meta name="copyright" content="Design/Code: Vit Dlouhy [Nuvio - www.nuvio.cz]; e-mail: vit.dlouhy@nuvio.cz" />
<title>WAVES 1.0!</title>
<meta name="description" content="..." />
<meta name="keywords" content="..." />
<link rel="index" href="./" title="Home" />
<link rel="stylesheet" media="screen,projection" type="text/css" href="./css/main.css" />
<link rel="stylesheet" media="print" type="text/css" href="./css/print.css" />
<link rel="stylesheet" media="aural" type="text/css" href="./css/aural.css" />
<script type="text/javascript" src="js/jquery-1.2.6.pack.js"></script>
<script language="javascript" type="text/javascript">
function getQueryParams( val )
{
//Use the window.location.search if we don't have a val.
var query = val || window.location.search;
query = query.split('?')[1]
var pairs = query.split('&');
var retval = {};
var check = [];
for( var i = 0; i < pairs.length; i++ )
{
check = pairs[i].split('=');
retval[check[0]] = check[1];
}
return retval;
}
var values = getQueryParams();
var v = values['id'];
$(function()
{
$.getJSON("getviewresults.php?id="+v, function(data)
{
$.each(data, function(entryIndex, entry)
{
var name = entry['tid'];
var user = entry['status'];
var p= entry['description'];
var f = entry['time'];
var stime = entry['stime'];
$('<tr> <td>'+name+'</td> <td > '+p+'</td> <td> '+user+'</td> <td>'+f+'</td> <td> <a href="staticlog.htm?id='+v+'&tid='+name+'&stime='+stime+'" target="_blank">Log </a> </td> <td><a href="staticslog.htm?id='+v+'&tid='+name+'&stime='+stime+'" target="_blank">Device Log </a> </td><td> <a href="viewethlog.php?id='+v+'&tid='+name+'&stime='+stime+'" target="_blank"> LAN </a> </td> </tr> ').appendTo('#container');
});
});
});
</script>
</head>
<body>
<!-- Main -->
<div id="main" class="box">
<!-- Header -->
<div id="header" style="left: 0px; width: 100%; top: 1px; height: 61px">
<!-- Logotyp -->
<h1 id="logo" style="left: 46px; top: 19px"><a href="./" title="CrystalX [Go to homepage]">WAVES 1.0!<strong>!</strong><span></span></a></h1>
<hr class="noscreen" />
<img src="Design/logo.jpg" style="right:0px; width: 134px; position: absolute;
top: -2px; height: 61px" />
</div> <!-- /header -->
<!-- Main menu (tabs) -->
<div id="tabs" class="noprint" style="width:100%; left: 0px; position: relative; top: 0px; height:24px">
<h3 class="noscreen">Navigation</h3>
<ul class="box">
<li><a href="home.php">Home<span class="tab-l"></span><span class="tab-r"></span></a></li>
<li><a href="package.php">Package<span class="tab-l"></span><span class="tab-r"></span></a></li>
<li ><a href="config.php">Configs<span class="tab-l"></span><span class="tab-r"></span></a></li>
<li><a href="live.php">Live<span class="tab-l"></span><span class="tab-r"></span></a></li>
<li id="active"><a href="results.php">Results<span class="tab-l"></span><span class="tab-r"></span></a></li>
<li><a href="logout.php">Log out<span class="tab-l"></span><span class="tab-r"></span></a></li>
</ul>
<hr class="noscreen" />
</div> <!-- /tabs -->
<!-- Page (2 columns) -->
<!-- /page -->
<div style="border-right: #336699 thin solid; border-top: #336699 thin solid; left: 0px;
border-left: #336699 thin solid; width: 100%; border-bottom: #336699 thin solid;
position: relative; top: 0px; height: 480px; overflow:auto">
<table id="container" style="left: 0px; width:100%; position: relative; ">
<tr>
<td style="width: 100px">
<strong>Testid</strong></td>
<td style="width: 382px">
<strong>Description</strong></td>
<td style="width: 100px">
<strong>Status</strong></td>
<td style="width: 100px">
<strong>Time Taken</strong></td>
<td style="width: 100px">
<strong>Log </strong></td>
<td style="width: 100px">
<strong>Device Log </strong></td>
<td style="width: 100px">
<strong>LAN </strong></td>
</tr>
</table>
</div>
</div>
<!-- Footer -->
<!-- /footer -->
<!-- /main -->
</body>
</html>
summary.htm文件只有脚本A的代码,这意味着它没有执行nnd,因此没有来自数据库的数据
我做错了什么?尝试将“viewresults.htm”重命名为“viewresults.php”,以便将其解析为php(您可以使用mod_rewrite在内部将viewresults.htm重定向到viewresults.php,以便您的URL不会更改)。打开您试图在浏览器中获取的URL,即
http://localhost/waves/viewresults.htm?id=X
(正确的值为X
)-并查看输出的内容
如果您仍然看到浏览器中显示的PHP代码,则您知道该文件未被解析为PHP。默认情况下,只有结尾带有.PHP
的文件名才会作为PHP执行
另外,我认为脚本A中的代码无论如何都不会正确执行,因为不会设置会话变量。因此我开始回答,但没有阅读您的代码,但现在看到您正在使用JavaScript从脚本A输出数据,而不是使用PHP 为什么不起作用:您正在运行时使用客户端JavaScript动态创建表。这将在浏览器中正确呈现,但不会通过curl呈现。libcurl不支持JavaScript 建议的解决方案 我建议您修改getviewresults.php并按照下面的步骤将输出缓冲到文件中,或者您也可以选择重写脚本以使用服务器端脚本
这是一个非常有趣的问题,谢谢。我试过了。但是没有用。Summary.htm只是输出文件。CURL在获取内容方面所做的主要工作。啊,那应该是viewresults而不是Summary它已经是一个php脚本。我将它命名为viewresults.phpDid您更改这一行:$url=“;to$url=“;(将.htm更改为.php)同意此评论。您正在尝试使用JavaScript获取ID。您需要用PHP重写它。除非您在其他地方使用此文件并试图避免重复,否则我看不到使用JavaScript的理由。谢谢您的支持好的先生!看来我们问题的作者使用的是jQuery,并且有一个返回JSON字符串。在您尝试从传统浏览器之外的地方访问它之前,这两件事是密切相关的。我仍然认为最简洁的解决方案是修改getviewresults.php,但另一个可行的选择是创建另一个php页面以提供所需的特定数据。
$ch = curl_init();
$url = "http://localhost/waves/viewresults.htm?id=".$id;
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
chdir("temp/file");
$myFile = "summary.htm";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $data);
fclose($fh);