首页 > 解决方案 > 使用 codeigniter 集成座位卖家 api。错误:ExceptionRequest 失败,代码 401:错误:OAUTH 验证失败

问题描述

我已经成功地对 api 文档http://api.seatseller.travel/docs/interface.html 中提供的 get 方法进行了 oauth1.0 身份验证,
但是我在上述文档 URL 中的 blockticket url 中遇到了问题:http:// api.seatseller.travel/blockTicket
我需要发布参数。我在codeigniter中使用了Oauth1.0 2Leg库。但它给了我错误:

ExceptionRequest 失败,代码为 401:错误:OAUTH 验证失败。

参考 Oauth 库的这个 url。https://github.com/jesstelford/OAuth-PHP

在 codeigniter/application/helpers 我创建了一个我编码的助手

 include_once APPPATH . 'libraries/OAuth/OAuthStore.php';
   include_once APPPATH . 'libraries/OAuth/OAuthRequester.php';

   function getbusUrlPost($url,$data) {
    $key = 'key';
    $secret = 'secret'; 
    $options = array('consumer_key' => $key, 'consumer_secret' => $secret);
    OAuthStore::instance("2Leg", $options);
    $method = "POST"; 
    $params =$data;

    //var_dump($params);
    try
    {
         // Obtain a request object for the request we want to make
        $request = new OAuthRequester($url, $method,$params);
        $result = $request->doRequest(0);

        // Sign the request, perform a curl request and return the results, 
        // throws OAuthException2 exception on an error
        // $result is an array of the form: array ('code'=>int, 
        'headers'=>array(), 'body'=>string)
        $result = $request->doRequest();

        $response = $result['body'];

        if ($response != 
        'oauth_token=requestkey&oauth_token_secret=requestsecret') 
        {
                echo $response;
        }
        else 
        {
            ECHO '------';
        }     
    }
    catch(OAuthException2 $e)
    {
            echo "Exception" . $e->getMessage();
    }

   }

在控制器中:

 <?php
       include_once APPPATH . 'libraries/REST_Controller.php';
       class BusBooking extends Rest_Controller {

    public function get_bus_blockTicket_post() {
         if(isset($_REQUEST['availableTripID']) && 
   isset($_REQUEST['seatname']) 
                 && isset($_REQUEST['fare']) && 
   isset($_REQUEST['ladiesSeat']) 
                 && isset($_REQUEST['name']) && isset($_REQUEST['mobile']) 
   && isset($_REQUEST['title']) 
                 && isset($_REQUEST['email']) && isset($_REQUEST['age']) && 
     isset($_REQUEST['gender'])){

             $url="http://api.seatseller.travel/blockTicket";

       $data=array(
           'availableTripID'=>$_REQUEST['availableTripID'],
                'seatname'=>$_REQUEST['seatname'],
                'fare'=>$_REQUEST['fare'], 
                'ladiesSeat'=>$_REQUEST['ladiesSeat'],             
                   'name'=>$_REQUEST['name'],
                    'mobile'=>$_REQUEST['mobile'],
                     'title'=>$_REQUEST['title'],
                     'email'=>$_REQUEST['email'],
                     'age'=>$_REQUEST['age'],  
                    'gender'=>$_REQUEST['gender']                                    

        );
       $sources= getbusUrlPost($url, $data); 
    }
  }

我也尝试过卷曲。但是得到同样的错误。用 curl 检查下面的代码。

function getBusblock($data) {

   $url="http://api.seatseller.travel/blockTicket";
   $header[]= 'Content-Type: application/x-www-form-urlencoded';
   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, $url); 
   curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
   curl_setopt($ch, CURLOPT_POST,true);
   curl_setopt($ch, CURLOPT_POSTFIELDS, 
 array('oauth_consumer_key'=>"key",'oauth_signature_method'=>"HMAC- 
 SHA1",'oauth_timestamp'=>'1557752217','oauth_nonce'=>'5cd969995be23','oauth_version'=>'1.0','oauth_signature'=>'DsMdvOOUI57VTkx5VQokUmi9rvw%3D&'));
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  $result = curl_exec($ch);
  if(curl_error($ch))
  {
    echo 'error:' . curl_error($ch);
  }
   echo "<pre>";
   echo json_encode($result);
   echo "</pre>";
   exit;
  }

请帮助我。我已经尝试了很多,但我遇到了 Oauth 验证失败的问题。

我期待 json 格式的响应以及所有票证详细信息。但是得到这个输出:ExceptionRequest failed with code 401: Error: OAUTH 验证失败。当用邮递员打网络服务时。

标签: phpapicodeigniter

解决方案


利用

CURLOPT_HTTPHEADER

代替

CURLOPT_POSTFIELDS


推荐阅读