如何使用cURL通过HTTP标头发送和接收数据?

时间:2014-05-13 00:28:08

标签: php api curl token

我正在尝试构建一个简单的API,以允许客户端通过HTTPS向我发送数据。 我创建了一个将使用用户名/密码的类,然后查找数据库。如果找到用户,则它会发出令牌。然后,令牌将通过HTTP标头发送回请求者。

一旦发回用户名,密码和令牌,脚本就会通过$_POST请求读取客户端发送的数据并对其进行处理。

我遇到的挑战是通过cURL将令牌发送给请求者并接收USERNAMEPASSWORD&正确地从HTTP标头中TOKEN

我的问题是如何通过generateToken()方法中的HTTP标头正确发送令牌?另外,如何在请求完成后读取HTTP标头?

以下是我的课程:api.php文件

<?php
require('../classes/connection.php');

class api {

    private $user_name;
    private $user_password;
    private $user_token;
    private $db;
    private $keepAlive = 120; //2 minutes = 120 seconds
    private $authorizes = false;
    private $token = '';
    private $ch;
    private $user_ready = false;

    function api($database, $server){

        //establish a database connection
        $this->db = new connection($database, $server);
        $this->ch = curl_init();

        //read user_name, password, token from the header and set it
        if(isset($_SERVER['API-User-Name']))
            $this->user_name = $_SERVER['API-User-Name'];

        if(isset($_SERVER['API-User-Password']))
            $this->user_password = $_SERVER['API-User-Password'];

        if(isset($_SERVER['API-User-Token']))
            $this->user_token = $_SERVER['API-User-Token'];

        //check if the user is allowed
        if(  $this->authenticateAccess() === true  ){
            $this->authorizes = true;

            //ensure the token is valid otherwise generate a new token
            if(     $this->isValidToken()   )
                $this->user_ready = true;
            else 
                $this->generateToken();
        }
    }

    //return weather to process the send data
    public function isUserReady(){
        return $this->user_ready;
    }

    //return weather the user is authorized
    private function isAutherized(){
        return $this->authorizes;
    }

    //return the set token
    private function getToken(){
        return $this->token;
    }

    //check if the requester is authorized to access the system
    private function authenticateAccess(){

        //unauthorized old session
        $this->unautherizeExpiredTokens();

        if( $this->ch === false) 
            return false;

        if( empty($this->user_name)  || empty($this->user_password)  )
            return false;

        //ensure HTTPS is used  
        if( !isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != 'on') 
            return false;
         //read the user information                
         $get_user = $this->db->getDataSet('SELECT ip_addreses, user_password, token_expires_on, current_token
                                            FROM api_users
                                            WHERE user_name = ?
                                            LIMIT 1', array($this->user_name));

         if( count($get_user) != 1)
            return false;

         $data = $get_user[0];

         //remove bad values if any
         $ip_addreses = preg_replace("/[^0-9,.]/", "", $data['ip_addreses']);

         $allowed_ips = explode(',', $ip_addreses);

        //ensure the IP address is allowed
        if( !isset($_SERVER['REMOTE_ADDR']) || !in_array($_SERVER['REMOTE_ADDR'], $allowed_ips) )
            return false;

        //check if the password is valid    
        if( password_verify($this->password, $data['user_password'] ) )
            return true;
        else        
            return false;
    }


    //check if the token is valid
    private function isValidToken(){

        if(     !$this->isAutherized()  )
            return false;

        //unauthorized old session
        $this->unautherizeExpiredTokens();


        if( empty($this->user_token) )
            return false;

         $get_user = $this->db->getDataSet('SELECT token_expires_on, current_token
                                            FROM api_users
                                            WHERE user_name = ? AND current_token = ?
                                            LIMIT 1', array($this->user_name, $this->user_token ));

         if( count($get_user) != 1)
            return false;

        $data = $get_user[0];

        if( empty($data['token_expires_on']) || $data['current_token'] != $this->user_token )
            return false;

        //make sure that the token is not expired
        if( !empty($data['token_expires_on']) && time() > $data['token_expires_on'])
            return false;   

    }

    //generate a new token
    private function generateToken(){
            //generate a token          
            $token = md5(uniqid(mt_rand(), true));
            //set expiration date for this token
            $expire_on = time() + $this->keepAlive;

            //Save the new token in the database with expiration time = $this->keepAlive seconds
            $update = $this->db->processQuery('UPDATE api_users
                                               SET current_ip = ?,
                                               current_token = ?,
                                               token_expites_on = ?
                                               WHERE user_name = ?', array($_SERVER['REMOTE_ADDR'], $token, $expire_on ));
            //if the token is saved in the database then send the new token via cURL header.                                   
            if($update){
                //set the token as a header value and then sent it back to the requester.
                $this->token = $token;
                $curl_header = array();
                $curl_header[] = 'API-User-Token: ' . $token;
                curl_setopt($this->ch, CURLOPT_HEADER, true);
                curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($this->ch, CURLOPT_HTTPHEADER, $curl_header);

                return $token;
            } else
                return false;   
    }


    //remove old tokens
    private function unautherizeExpiredTokens(){

        $this->db->processQuery('UPDATE api_users
                                 SET current_ip = NULL,
                                 current_token = NULL,
                                 token_expites_on = NULL
                                 WHERE token_expites_on IS NOT NULL AND token_expites_on <= ?', array( time() ) );  

    }

}

?>

要使用此类,我将从API访问链接执行以下操作。一旦我弄清楚如何准备http数据,就不需要将$username$password传递给类,而是从标题中准备好它。

因此,access.php文件将如下所示     

include('api.php');


$request = new api('database_name','serverIPaddress');


if( $request->isUserReady() ){
    //process transaction all transactions
    $_POST['notes'];  //// take the data validated it and then insert into the database

    echo 'Bingo!';

} else {
    echo 'You are not authorized to use this API';
}

?>

要使用此API,客户端必须调用它,因此client.php文件将如下所示:

<?php

$curl_header = array();
$curl_header[] = 'API-User-Name: test';
$curl_header[] = 'API-User-Password: password';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://mydomainname.com/api/access.php");
//curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $curl_header);
$result = curl_exec($ch);


print_r($result);
curl_close($ch);


?>

1 个答案:

答案 0 :(得分:2)

只需使用CURLOPT_HTTPHEADER

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  'X-THING_ONE: abcdefghijklmnopqrstuvwxyz',
  'X-THING_TWO: 12345678910'
));

我更喜欢将标题设置为curl_setopt之外的数组,如下所示:

$curl_headers = array();

$curl_headers[] = 'X-THING_ONE: abcdefghijklmnopqrstuvwxyz';
$curl_headers[] = 'X-THING_TWO: 12345678910';

curl_setopt($ch, CURLOPT_HTTPHEADER, $curl_headers);

编辑:好的,看起来您知道CURLOPT_HEADER是如何运作的。但是看看你的代码,这里似乎有一个错字。

curl_setopt($this->ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->ch, CURLOPT_HTTPHEADER, array('API-Token: ' . $this->getToken() ));

为什么$this->ch中有CURLOPT_HEADER&amp; CURLOPT_HTTPHEADER只是$ch CURLOPT_RETURNTRANSFER而已curl_setopt($this->ch, CURLOPT_HEADER, true); curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($this->ch, CURLOPT_HTTPHEADER, array('API-Token: ' . $this->getToken() )); 不应该是这样吗?

$_SERVER['X-API-User-Name'];
$_SERVER['X-API-User-Password'];
$_SERVER['X-API-User-Token'];

编辑:除了这个错字之外,原版海报似乎需要知道如何在接收方获取参数:

  

另外,如何在请求发出后读取HTTP标头?

易。它们可以通过PHP中的$_SERVER预定义变量访问。所以你会像这样抓住它们:

echo '<pre>';
print_r($_SERVER);
echo '</pre>';

您可以通过执行以下操作来检查调试时传递的内容:

{{1}}