错误:使用Codeigniter集成座位卖方api。错误:ExceptionRequest失败,代码为401:错误:OAUTH验证失败

时间:2019-05-22 12:06:20

标签: php api codeigniter

我已成功完成api文档中提供的get方法的oauth1.0身份验证 http://api.seatseller.travel/docs/interface.html
但是我在上述文档URL:http://api.seatseller.travel/blockTicket的可疑URL中遇到问题。
我需要一起传递参数。我在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失败,代码为401:错误:OAUTH验证失败。 用邮递员访问Web服务时。

1 个答案:

答案 0 :(得分:0)

使用

CURLOPT_HTTPHEADER

代替

CURLOPT_POSTFIELDS