What is the difference in the way php curl and browsers access scripts?

what is the difference between the way php curl and browsers access scripts?
now requests to an api can correctly return a value by accessing the script in a browser, while sending a post with php-curl returns an error saying that the value of content-length has not been set. What"s the difference between the two? What does the nginx agent do during the process?

$topic = "topic";
    $projectId = "projectid";
    $title = "hahaha";
    $content = "lol";

    $payload = array(
        "message" => array(
            "topic" => $topic,
            "notification" => array(
                "title" => $title,
                "body" => $content,
            )
        )
    );

    $json = json_encode(trim($payload));
    $headers = array(
        "Authorization:Bearer ".$this->getFcmApiAccessToken(),
        "Content-Type: application/json; UTF-8",
        "Content-Length:".strlen($json),//"Content-length:0"
    );

    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL,"https://fcm.googleapis.com/v1/projects/{$projectId}/messages:send");
    curl_setopt($ch,CURLOPT_POST, true );
    curl_setopt($ch,CURLOPT_HEADER, 1);
    curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch,CURLOPT_POSTFIELDS, $json);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);

    $result = curl_exec($ch);
    curl_close($ch);

    var_dump($result);exit;

Mar.13,2021

content-length error just set the request header


one is that the browser will process your header request, and one needs to be assembled by yourself, but if there is something like cookie, you still have to configure curl


. The curl of

php generally does not need to set content-length.

it is recommended that you post the curl-related code.

Menu