仅使用PHP发送类似AJAX的发布请求

时间:2013-02-14 11:58:30

标签: php algorithm function http-post

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

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

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

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

先谢谢你们。

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

2 个答案:

答案 0 :(得分:9)

您应该使用curl。你的功能将是这样的:

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

然后你应该这样称呼它:

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

答案 1 :(得分:0)

我将给你一些示例类,在下面的例子中,你也可以使用它作为get和post post。我希望这会对你有帮助。!

 /*
  for your reference . Please provide arguement like this,
  $requestBody = array(
                    'action' => $_POST['action'],
                    'method'=> $_POST['method'],
                    'amount'=> $_POST['amount'],
                    'description'=> $_POST['description']
                   );
 $http = "http://localhost/test-folder/source/signup.php";
 $resp = Curl::postAuth($http,$requestBody);
 */   

class Curl {
// without header
public static function post($http,$requestBody){

     $curl = curl_init();
        // Set some options - we are passing in a useragent too here
        curl_setopt_array($curl, array(
            CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_URL => $http ,
            CURLOPT_USERAGENT => 'From Front End',
            CURLOPT_POST => 1,
            CURLOPT_POSTFIELDS => $requestBody
        ));           
        // Send the request & save response to $resp
        $resp = curl_exec($curl);
        // Close request to clear up some resources           
        curl_close($curl);
        return $resp;
}
// with authorization header
public static function postAuth($http,$requestBody,$token){
    if(!isset($token)){
        $resposne = new stdClass();
        $resposne->code = 400;
        $resposne-> message = "auth not found";
       return json_encode($resposne);
    }
     $curl = curl_init();
      $headers = array(                
            'auth-token: '.$token,
        );
        // Set some options - we are passing in a useragent too here
        curl_setopt_array($curl, array(
            CURLOPT_HTTPHEADER  => $headers ,
            CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_URL => $http ,
            CURLOPT_USERAGENT => 'From Front End',
            CURLOPT_POST => 1,
            CURLOPT_POSTFIELDS => $requestBody
        ));


        // Send the request & save response to $resp
        $resp = curl_exec($curl);
        // Close request to clear up some resources           
        curl_close($curl);
        return $resp;
}

}