Home > Article > Backend Development > PHP uses CURL to access the remote URL and an error occurs: Cannot modify header information...
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>
An error occurred:
<code>Cannot modify header information - headers already sent by...</code>
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.
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>
An error occurred:
<code>Cannot modify header information - headers already sent by...</code>
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.