Laravel中的同一个端口在我的代码中不起作用

时间:2019-01-07 12:10:34

标签: php laravel

我正在使用cUrl在我的网站中设置新的fitur登录API。

当我在Postman中运行时,它正在工作(滚动2)。但是当我在我的网站上运行时,使用cUrl无法正常工作并仍在加载。如果我没有设置超时,它将继续加载直到无限时间,如screnshoot 1中一样。

image 1 : when i run in my website 图片1:当我在网站上运行时

image 2 : when i run in Postman 图片2:当我在邮递员中运行时

这是我的代码 用于从API / cUrl请求进行登录的登录控制器

public function login()
{
    if (Auth::attempt(['email' => request('email'), 'password' => request('password')])) {
        $user = Auth::user();
        return response()->json(['result' => true, 'message' => "heyho" ], 200);
        // $token =  $user->createToken('nApp')->accessToken;
        // return response()->json(['result' => true, 'message' => $token ], $this->successStatus);
    } else {
        return response()->json(['result'=> false, 'message' => 'Unauthorised'], 401);
    }
}

这是我的代码cUrl流程/请求。

public function tes()
{
    $curl = curl_init();

    curl_setopt_array($curl, array(
        CURLOPT_PORT => "8001",
        CURLOPT_URL => "http://localhost:8001/api/login",
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 30,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "POST",
        CURLOPT_POSTFIELDS => "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"email\"\r\n\r\email@gmail.com\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"password\"\r\n\r\bbbbb\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--",
        CURLOPT_HTTPHEADER => array(
            "cache-control: no-cache",
            "content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
            "postman-token: 3546ebed-2016-df32-6d9d-91cdfd43066a"
        ),
    ));

    $response = curl_exec($curl);
    $err = curl_error($curl);

    curl_close($curl);

    if ($err) {
        echo "cURL Error #:" . $err;
    } else {
        echo $response;
    }
    // return url('/')."/api/login";
}

1 个答案:

答案 0 :(得分:0)

Your request is "POST" then how you can see the result in web page 

Only "GET" request only show the results on the web page 

"GET" : In this case we can pass the parameter in the url 
        like : https//:localhost:8000/api/user/1


"POST" : In this case we pass the body in the request body so we need postman to pass the request body"

For more info you can check https://www.w3schools.com/tags/ref_httpmethods.asp