Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

send request with x-auth-token in header php

Tags:

php

yii

How can I send "x-auth-token" param to server with headers in YII.

I have this code

$data = array('customerId' => $userId);

        $getdata = http_build_query(
            $data 
        );      

        $options = array('http' =>
             array(
                'method'  => 'GET',
                'header'  => "Content-type: application/x-www-form-urlencoded\r\n".
                " Authorization: x-auth-token ".$token." \r\n",
                'content' => $getdata
            )
        );

        $context  = stream_context_create($options);
        $result = file_get_contents('url?'.$getdata, false, $context);

in Android we are sending data like this request.addHeader("x-auth-token", token);

I have no access to server, I am just sending requests and getting data. But after login I need to send login token to get data, but it is returns me 403.

So I think it is not sending the token. How can I do that?

like image 564
Anna Gabrielyan Avatar asked Jun 29 '15 14:06

Anna Gabrielyan


1 Answers

$headers = array();
$headers[] = "x-auth-token: $token";
$headers[] = 'Content-Type: application/x-www-form-urlencoded; charset=utf-8';
$state_ch = curl_init();
    curl_setopt($state_ch, CURLOPT_URL,"url");
    curl_setopt($state_ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($state_ch, CURLOPT_HTTPHEADER, $headers);
    $state_result = curl_exec ($state_ch);
    $state_result = json_decode($state_result); 

I have done it with CURL

like image 167
Anna Gabrielyan Avatar answered Nov 08 '22 01:11

Anna Gabrielyan