PHP CURLOPT_WRITEFUNCTION은 헤더 정보를 본문에 반환합니다.
P粉799885311
P粉799885311 2024-03-19 19:20:53
0
1
411

curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'curl_write_flush') 时,我将标头插入到 $body 中。但如果没有 CURLOPT_WRITEFUNCTION 一切正常。我该怎么做才能阻止标头插入 $body를 포함하면?

<?php

    $ch = curl_init();
    
    curl_setopt($ch, CURLOPT_URL, "https://stackoverflow.com");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

    function curl_write_flush($curl_handle, $chunk) { 
        echo $chunk;
        ob_flush();
        flush();
        return strlen($chunk);
    }
    curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'curl_write_flush'); // WORKS WITHOUT

    $response = curl_exec($ch);
    $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);

    curl_close($ch);

    $headers = substr($response, 0, $header_size);
    $body = substr($response, $header_size);

    die($body);

?>

P粉799885311
P粉799885311

모든 응답(1)
P粉262113569

https://curl.se/libcurl/c/CURLOPT_WRITEFUNCTION.html

curl_setopt($ch, CURLOPT_HEADER, 1);을 제거하거나 콜백 데이터에서 헤더를 직접 구문 분석해야 합니다.

다음 답변에 표시된 대로 CURLOPT_HEADERFUNCTION를 사용할 수도 있습니다.

https://stackoverflow.com/a/41135574/1064767

최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!