Home  >  Article  >  Backend Development  >  PHP uses CURL to access the remote URL and an error occurs: Cannot modify header information...

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

WBOY
WBOYOriginal
2016-08-23 09:17:481415browse

curl 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;

An error occurred:

Cannot modify header information - headers already sent by...

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:

        $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;

An error occurred:

Cannot modify header information - headers already sent by...

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.

Statement:
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