PHP uses CURL to access the remote URL and an error occurs: Cannot modify header information...

WBOY
Release: 2016-08-23 09:17:48
Original
1520 people have browsed it

curl code:

<code>        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);  //请求URL
        curl_setopt($ch, CURLOPT_HEADER, 0);  //禁止输出头部信息
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //获取信息以文件流的格式返回
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        if( !is_null($postData) ){
            curl_setopt($ch, CURLOPT_POST, 1);  //POST提交
            curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
        }
        $return = curl_exec($ch);
        curl_close($ch);
        return $return;</code>
Copy after login
Copy after login

An error occurred:

<code>Cannot modify header information - headers already sent by...</code>
Copy after login
Copy after login

No errors occurred when accessing the URL directly in the browser.

I also searched for solutions to the above problems on the Internet. Most of them involve modifying PHP files or modifying PHP configuration. . . But my idea is to use CURL to achieve the same effect as browser access? How to achieve this? Because browsing it accesses that URL no error is generated.

Reply content:

curl code:

<code>        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);  //请求URL
        curl_setopt($ch, CURLOPT_HEADER, 0);  //禁止输出头部信息
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //获取信息以文件流的格式返回
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        if( !is_null($postData) ){
            curl_setopt($ch, CURLOPT_POST, 1);  //POST提交
            curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
        }
        $return = curl_exec($ch);
        curl_close($ch);
        return $return;</code>
Copy after login
Copy after login

An error occurred:

<code>Cannot modify header information - headers already sent by...</code>
Copy after login
Copy after login

No errors occurred when accessing the URL directly in the browser.

I also searched for solutions to the above problems on the Internet. Most of them involve modifying PHP files or modifying PHP configuration. . . But my idea is to use CURL to achieve the same effect as browser access? How to achieve this? Since browsing it accesses that URL no error is generated.

You need a library that encapsulates curl

I checked stackoverflow, maybe your file encoding has a BOM header.
Try changing the encoding to UTF-8 without BOM format.

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template