English 中文(简体)
• 如何与控制器内的厨师提出要求
原标题:How to make POST (API) request with cookies inside controller | Laravel 9.x

I m working with Omada Controller (External Portal Server). That is, to register users through the EAP. The device must be allowed to use WiFi. I use following documentation:

https://www.tp-link.com/ru/support/faq/3231/ I tested the requests through Postman. Everything is working. However, it is necessary to send a cookie in the request to authorize the device after logging in.

我在Laravel写道: 但是,没有储存或发送 co。

public function AuthFree(Request $req)
    {
        $inputs = $req->all();
        //IP Address of Controller
        $endPoint =  https://CONTROLLER_API:PORT/CONTROLLER_ID/api/v2/hotspot/extPortal/auth ;
        $operatorData = self::getCSRFToken();
        $csrfToken = $operatorData->object()->result->token;
        $cookies = $operatorData->cookies()->getCookieByName( TPEAP_SESSIONID )->toArray();//->getValue();
        $deviceBody = [
             clientMac  => $inputs[ clientMac ],
             apMac  => $inputs[ apMac ],
             ssidName  => $inputs[ ssidName ],
             radioId  => $inputs[ radioId ],
             time  => 1859960742,
             authType  => 4,
        ];

        $authDevice = Http::withCookies($cookies, $endPoint)
        ->timeout(30)->withOptions([
             verify  => false,
        ])
        ->withHeaders([
                 Content-Type  =>  application/json ,
                 Accept  =>  application/json ,
                 Csrf-Token  => $csrfToken,
            ])
            ->post($endPoint, $deviceBody);
        dd($authDevice);
    }

//Login operator.
public function getCSRFToken()
    {
        $authOperator = Http::accept( application/json )->timeout(15)->withOptions([
             verify  => false,
        ])
            ->post( https://IP:PORT/ID/api/v2/hotspot/login , [
                 name  =>  USERNAME ,
                 password  =>  PASSWORD ,
            ]);
            
        return $authOperator;
    }

i 还写成以下法典:

$ch = curl_init();
        // Set return to a value, not return to page
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        // Set up cookies.
        curl_setopt($ch, CURLOPT_COOKIEJAR, request()->cookie( TPEAP_SESSIONID ));
        curl_setopt($ch, CURLOPT_COOKIEFILE, request()->cookie( TPEAP_SESSIONID ));
        // Allow Self Signed Certs
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        // API Call
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($authInfo));
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        $res = curl_exec($ch);
        $resObj = json_decode($res);
        return $resObj;

但这也会产生错误。

The operator enters the system and receives a token through the getCSRFToken function mentioned above. A COOKIE is returned along with the token. I need to send TOKEN and COOKIES together to authorize the device. Meanwhile, the getCSRFToken function is running and returning tokens. But authDevice is returning an error. Because it can t get Header and Cookies.

我的问题是:我如何拯救 co,我如何能够在请求中添加这些 co,并送它们?

我所写的法典有什么错误?

问题回答

就我所知 COOKIEJAR and CURLOPT_COOKIEFILE 需要同样的档案,例如:

curl_setopt($ch, CURLOPT_COOKIEJAR, $temp_cookie_file);
curl_setopt($ch, CURLOPT_COOKIEFILE,$temp_cookie_file);

I m struggling with the setup of an external portalt too. Get a token, I can see the TPEAP_SESSIONID in the $temp_cookie_file , but get in the authorization step: "[errorCode] => -1 [msg] => General error."





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签