仅使用PHP发送类似AJAX的发布请求 [英] Send AJAX-like post request using PHP only

查看:119
本文介绍了仅使用PHP发送类似AJAX的发布请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在PHP中使用一些自动化脚本(无HTML!)。
我有两个PHP文件。一个是执行脚本,另一个接收$ _POST数据并返回信息。
问题是如何从一个PHP脚本发送POST到另一个PHP脚本,获取返回变量并继续处理第一个没有HTML表单并且没有重定向的脚本。
在不同的条件下,我需要从第一个PHP文件到另一个请求几次,并根据请求返回不同类型的数据。
我有这样的东西:

 <?php // action.php(第一个PHP脚本)
/ *
做一些东西
* /
$ data = sendPost('get_info'); //发送POST到getinfo.php属性['get_info']并返回数据另一个文件
$ mysqli-> query(INSERT INTO domains(id,name,address,email)
VALUES('。$ data ['id']。','。$数据['name']。','。$ data ['address']。','。$ data ['email']。'))或死亡(mysqli_error($ mysqli));
/ *
继续做一些事情
* /
$ data2 = sendPost('what_is_the_time'); //发送POST到getinfo.php属性['what_is_the_time']和返回来自另一个文件的时间数据

sendPost('get_info'或'what_is_the_time'){
//发送所需属性
返回$ data; }
?>

我想我需要一些函数来调用属性,发送post请求并返回数据根据要求。
第二个PHP文件:

 <?php // getinfo.php(另一个PHP脚本)
if($ _ POST ['get_info']){
//做一些动作
$ data = anotherFunction();
返回$ data;

if($ _ POST ['what_is_the_time']){
$ time = time();
返回$ time;


函数anotherFunction(){
//做一些事情
return $ result;
}
?>

在此先感谢您们。



更新: 好。 curl方法正在获取php文件的输出。如何仅返回一个$ data变量而不是整个输出?

php.net/curlrel =noreferrertitle =curl> curl 。你的函数将如下所示:

$ p $ function sendPost($ data){
$ ch = curl_init();
//你应该在这里放置你的getinfo.php脚本的url
curl_setopt($ ch,CURLOPT_URL,getinfo.php);
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ ch,CURLOPT_POST,1);
curl_setopt($ ch,CURLOPT_POSTFIELDS,$ data);
$ result = curl_exec($ ch);
curl_close($ ch);
返回$ result;
}

然后您应该这样调用它:

  $ data = sendPost(array('get_info'=> 1)); 


I'm currently working on some automatization script in PHP (No HTML!). I have two PHP files. One is executing the script, and another one receive $_POST data and returns information. The question is how from one PHP script to send POST to another PHP script, get return variables and continue working on that first script without HTML form and no redirects. I need to make requests a couple of times from first PHP file to another under different conditions and return different type of data, depending on request. I have something like this:

<?php // action.php  (first PHP script)
/* 
    doing some stuff
*/
$data = sendPost('get_info');// send POST to getinfo.php with attribute ['get_info'] and return data from another file
$mysqli->query("INSERT INTO domains (id, name, address, email)
        VALUES('".$data['id']."', '".$data['name']."', '".$data['address']."', '".$data['email']."')") or die(mysqli_error($mysqli));
/* 
    continue doing some stuff
*/
$data2 = sendPost('what_is_the_time');// send POST to getinfo.php with attribute ['what_is_the_time'] and return time data from another file

sendPost('get_info' or 'what_is_the_time'){
//do post with desired attribute
return $data; }
?>

I think i need some function that will be called with an attribute, sending post request and returning data based on request. And the second PHP file:

<?php // getinfo.php (another PHP script)
   if($_POST['get_info']){
       //do some actions 
       $data = anotherFunction();
       return $data;
   }
   if($_POST['what_is_the_time']){
       $time = time();
       return $time;
   }

   function anotherFunction(){
   //do some stuff
   return $result;
   }
?>

Thanks in advance guys.

Update: OK. the curl method is fetching the output of php file. How to just return a $data variable instead of whole output?

解决方案

You should use curl. your function will be like this:

function sendPost($data) {
    $ch = curl_init();
    // you should put here url of your getinfo.php script
    curl_setopt($ch, CURLOPT_URL, "getinfo.php");
    curl_setopt($ch,  CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    $result = curl_exec ($ch); 
    curl_close ($ch); 
    return $result; 
}

Then you should call it this way:

$data = sendPost( array('get_info'=>1) );

这篇关于仅使用PHP发送类似AJAX的发布请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆