普通网友 2016-06-01 10:27
浏览 35

从远程php文件调用并执行javascript文件

I have an angularjs service that sends a rest api link to a php file, the problem is that , this php file is calling another php file which contains a javascript code. When I execute the code in the service, it executes the php file , but the javascript code is just printed out without being executed. It's been a whole day of research but without any solution. Please can you tell me where the problem is? I have used require 'secondFile.php'; and include 'secondFile.php'; , I have also tried to bring the whole javascript code from the second file to the first file, so that I call the javascript locally, but it always prints the code without executing it. Here is my code:

The angularjs service function:

var link = 'http://path/to/first.php';
            $http.post(link, {idf : newDoc.idf})
            .success(function (res){
                console.log(res); 
            }).error(function (err) {
              console.log(err);
            });

the first php file, which is a remote file

         <?php 
            if (isset($_SERVER['HTTP_ORIGIN'])) {
     header("Access-Control-Allow-Origin:          {$_SERVER['HTTP_ORIGIN']}");
                     header('Access-Control-Allow-Credentials: true');
                     header('Access-Control-Max-Age: 86400');    
             }

             // Access-Control headers are received during OPTIONS requests
             if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {

                     if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
                             header("Access-Control-Allow-Methods: GET, POST, OPTIONS");

                     if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
                             header("Access-Control-Allow-Headers:        {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");

                     exit(0);
             }

             $method = $_SERVER['REQUEST_METHOD'];
             //$request = explode('/', trim($_SERVER['PATH_INFO'],'/'));
             $input = json_decode(file_get_contents('php://input'),true);

             $postdata = file_get_contents("php://input");
             $request = json_decode($postdata);
             var_dump($request);
             $idf= $request->idf;
             $val= $request->val;

                   switch ($method) {
                     case 'POST':
                              insertit($idf, $val);
                             break;
                     case 'PUT':
                              updateit();
                             break;
                     case 'REMOVE':
                              removeit();
                             break;
                   }

             }
     catch(PDOExecption $pe) {
         echo "okay";
         print "ERROR!".$pe->getMessage();
         die();
        }
function insertit($idf, $val) {
        include 'connect.php';

        $dataBilan = array($val);
    $stmt = $DB->prepare("INSERT INTO `Test` (val) VALUES (?)");
    $stmt->execute($dataBilan);
            $last = $DB->lastInsertId();
            $idf= $last;
    $info_bilan= array(
             'val' =>$val
            );
    $data = array(
   'idf'=>$last,
   'action'=>'update',
   'table'=>'activite',
   'data' =>$info_bilan
    );
            require 'second.php';
            echo "okay done inserting";
  }

and second.php:

    <script src="pouchdb-5.3.1.min.js"></script><script type="text/javascript" language="javascript">
var dbRemote = new PouchDB('http://localhost:5984/Mydatabase');
        var myDocs;
        var value = <?php echo json_encode($data); ?>;
        console.log(JSON.stringify(value));


    if((value.idf != null) && (value.idf != 0))
        {

                console.log("-------here we go----");
                dbRemote.allDocs({include_docs: true}).then(function (res) {
                myDocs = res.rows.map(function (row) {

                    return row.doc; });

                            onUpdate(value);

                });

     }
 }
    function binarySearch(arr, docId) {
            var low = 0, high = arr.length, mid;
            while (low < high) {
                mid = (low + high) >>> 1; // faster version of Math.floor((low + high) / 2)
                arr[mid].id < docId ? low = mid + 1 : high = mid
            }
            return low;
        }

    function onUpdate(value)
        {
            console.log("couchdb's "+ myDocs.length);
            if(myDocs.length !=0)
            {
                console.log("haha");
                 var index = binarySearch(myDocs, value.idf);
                 var doc = myDocs[index];

            if(action== "update")
             {
                dbRemote.put(doc).then (function () {
                    console.log("Updated! ");
                }).catch(function (err) {
                    console.log(err);
                });
             }
         }
    }

</script>

The content of second.php file is just printed out but it's not executed. When I execute first.php everything goes right , but when I execute the service I just see the printed file. Please any ideas?

  • 写回答

1条回答 默认 最新

  • dongzhong1891 2016-06-01 10:59
    关注

    Example:

    $connection = ssh2_connect('shell.example.com', 22);
    ssh2_auth_password($connection, 'username', 'password');
    
    $stream = ssh2_exec($connection, '/usr/local/bin/php -i');
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画