它是一种在PHP文件中运行javascript函数并将输出发送到客户端的方法吗?


is it a way to run javascript function in php file and send the output to client side?

我在服务器上的php文件中有一个javascript函数,我想通过AJAX发送数据,之后,服务器(php文件)获得数据作为javascript函数输入,从而运行函数,获得结果后将其发送到客户端。换句话说,我想在服务器端运行javascript函数(通过使用php和ajax),而不是在客户端。有办法做到吗?(更多信息web服务器是Apache)

您可以使用php的内置函数,如file_get_contents(), file()等

实际上它比javascript有更多的特性。请看下面的推文示例:

  function tweet($message, $username, $password) 
  { 
    $context = stream_context_create(array( 
      'http' => array( 
        'method'  => 'POST', 
        'header'  => sprintf("Authorization: Basic %s'r'n", base64_encode($username.':'.$password)). 
                     "Content-type: application/x-www-form-urlencoded'r'n", 
        'content' => http_build_query(array('status' => $message)), 
        'timeout' => 5, 
      ), 
    )); 
    $ret = file_get_contents('http://twitter.com/statuses/update.xml', false, $context); 
    return $ret;
  }