开发者

Unable to execute a php script through CURL

开发者 https://www.devze.com 2022-12-09 19:37 出处:网络
I have a PHP script A which displays data from database. I have another another script B which gets the content of script A .But I need the content after script A is executed . But this is not happe

I have a PHP script A which displays data from database.

I have another another script B which gets the content of script A .But I need the content after script A is executed . But this is not happending .

// Script 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: 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">
           开发者_StackOverflow             <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>

// Script 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);

The file summary.htm just has the code of the script A , which means it has not executed nnd hence has not data from the database .

What am I doing wrong ?


So I started to answer without reading your code but now see that you are using JavaScript to output the data from Script A instead of using PHP.

Why it wont work: You are dynamically creating a table at run time using client side JavaScript. This will render properly in a browser but will not render via curl. libcurl does not support javascript.

Suggested Solution I would suggest that you modify getviewresults.php and follow this tutorial to buffer the output to a file or you could also optionally just rewrite Script A to use a server side script.

This was a really entertaining problem thank you.


Try renaming "viewresults.htm" to "viewresults.php" so it's parsed as PHP (you could use mod_rewrite to internally redirect viewresults.htm to viewresults.php so your URLs don't change).


Open the URL you're trying to get in your browser - i.e. http://localhost/waves/viewresults.htm?id=X (with correct value of X) - and see what content is output.

If you still see PHP code displayed in your browser then you know that the file is not being parsed as PHP. By default only filenames with .php on the end will be executed as PHP.

Also, I don't think the code in Script A is going to be executed properly anyway because the session variables won't be set.

0

精彩评论

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