2009-10-23 283 views
1

我有一個PHP腳本A,它顯示來自數據庫的數據。無法通過CURL執行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'); 

     } 
    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: [email protected]" /> 
     <meta name="copyright" content="Design/Code: Vit Dlouhy [Nuvio - www.nuvio.cz]; e-mail: [email protected]" /> 

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

//腳本B

  $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); 

文件summary.htm剛剛已經腳本A的碼,這意味着它未NND因此執行具有不來自數據庫的數據。

我在做什麼錯?

回答

3

所以我開始回答而沒有閱讀你的代碼,但現在看到你正在使用JavaScript來輸出腳本A的數據,而不是使用PHP。

爲什麼它不會工作:您在運行時使用客戶端JavaScript動態創建表。這將在瀏覽器中正確呈現,但不會通過curl呈現。 libcurl不支持JavaScript。

建議的解決方案 我建議你修改getviewresults.php並按照this tutorial緩衝輸出到文件,或者您也可以選擇性剛剛改寫腳本A使用服務器端腳本。

這真是一個有趣的問題,謝謝。

+0

同意此評論。您正嘗試使用JavaScript獲取ID。你需要用PHP重寫它。我沒有看到使用JavaScript的理由,除非您在其他地方使用此文件並嘗試避免重複。 –

+0

謝謝你的好評先生!看來我們問題的作者是使用jQuery並且有一個返回JSON字符串的PHP腳本。這兩件事情是齊頭並進的,直到你試圖從傳統瀏覽器之外的某個地方訪問它。我仍然認爲最簡潔的解決方案是修改getviewresults.php,但另一個可行的選擇是創建另一個php頁面來提供所需的特定數據。 –

0

嘗試將「viewresults.htm」重命名爲「viewresults.php」,以便將其解析爲PHP(您可以使用mod_rewrite將viewresults.htm內部重定向到viewresults.php,以便您的URL不會更改)。

+0

我試過了。但是這沒有幫助。 Summary.htm只是輸出文件。CURL在獲取內容方面的主要工作。 – Vidya

+0

啊,這應該是查看結果不是彙總 – Greg

+0

它已經是一個PHP腳本。我已將它命名爲viewresults.php – Vidya

0

打開您嘗試在瀏覽器中獲得的網址 - 即http://localhost/waves/viewresults.htm?id=X(正確的值爲X) - 並查看輸出了哪些內容。

如果你仍然看到PHP代碼顯示在瀏覽器中,那麼你知道該文件沒有被解析爲PHP。默認情況下,只有.php最後的文件名將作爲PHP執行。

此外,我不認爲腳本A中的代碼將被正確執行,因爲會話變量不會被設置。