search

Home  >  Q&A  >  body text

php - laravel uses curl request, the return value is considered to be a request and an error occurs

Requirement background:
The project needs to make a curl request to other platforms. I send my mobile phone number, and the other platforms send verification codes to the users and send me a successful receipt.
Question:
Local Debugging is no problem, but when going to the server, the $data data in the curl receipt, that is, $data = curl_exec($curl);, is regarded as a request, and the token of this $data must be verified. Look for his distribution rules in the routing file...
My troubleshooting ideas:
I first added such a sentence var_dump(Request: :path()):

public function handle($request, Closure $next)

{

var_dump(Request::path());
if ($this->isReading($request) || $this->shouldPassThrough($request) || $this->tokensMatch($request)) {
    return $this->addCookieToResponse($request, $next($request));
}

throw new TokenMismatchException;

}

The path printed out is the URL for sending the curl request, and then there is the platform URL for curl communication, which led to the reason why I reported csrf_token errors and path errors later.

The control of when I send curl requests The device method is:

/*
 * 发送验证码请求
 */

public function ajaxRquest(Request $request) {
    $input['tel_num'] = $request->input('tel');
    $url = config('Api');
    $url = $url['sendVerifyCode'];
    if ($request->input('code_attr') == 'submitInformation') {
        $input['union_event'] = 'submitInformation';
    } else {
        $input['union_event'] = 'queryProgress';
    }
    $result = $this->postCurlInfo($url, json_encode($input));
    return $result;
}

public function postCurlInfo($url, $data) {
    $curl = curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 30,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "POST",
        CURLOPT_POSTFIELDS => $data,
        CURLOPT_HTTPHEADER => array(
            "cache-control: no-cache",
            "content-type: application/json",
        ),
    ));
    return curl_exec($curl);
}

And the path I printed out is:

string(16) "Index/ajaxRquest" string(31) "v1/serviceCenter/sendVerifyCode"

The first string is the method path I used to send curl, and the latter one is the url to send the receipt.

I think it is very strange. The return of curl should not be regarded as a request! May I ask why this happens? Thank you!

过去多啦不再A梦过去多啦不再A梦2729 days ago1073

reply all(1)I'll reply

  • 过去多啦不再A梦

    过去多啦不再A梦2017-06-05 11:10:44

    I don’t understand your question, but to initiate an http request, direct curl operation is not recommended. You can try the zttp library recommended by laravel

    reply
    0
  • Cancelreply