2016-06-01 73 views
0

我有一個angularjs服務發送一個PHP文件的休息API鏈接,問題是,這個PHP文件調用另一個PHP文件,其中包含一個JavaScript代碼。當我在服務中執行代碼時,它會執行php文件,但是JavaScript代碼只是被打印出來而不被執行。這是一整天的研究,但沒有任何解決方案。請你能告訴我問題在哪裏?我已經使用了require 'secondFile.php';include 'secondFile.php';,我也試圖將第二個文件中的整個JavaScript代碼放到第一個文件中,這樣我就可以在本地調用JavaScript,但它總是打印代碼而不執行它。 這裏是我的代碼:從遠程php文件調用並執行一個JavaScript文件

的angularjs服務功能:

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

第一php文件,這是一個遠程文件

  <?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"; 
    } 

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

second.php文件的內容只是打印出來,但不會執行。當我執行first.php時,一切正常,但是當我執行服務時,我只能看到打印的文件。請有什麼想法?

+0

一些代碼樣本將是有益.. –

+0

結帳JSONP http://stackoverflow.com/questions/3839966/can-anyone-explain-what-jsonp-is-in-layman - 條目 – Mei

+0

請參閱編輯過的文章!我編寫了能幫助你理解我的問題的代碼部分。謝謝! – Mana

回答

0

實施例:

$connection = ssh2_connect('shell.example.com', 22); 
ssh2_auth_password($connection, 'username', 'password'); 

$stream = ssh2_exec($connection, '/usr/local/bin/php -i');