Is there a string length limit for php curl return value?

2.7k Views Asked by At

I have a piece of php code to curl an API endpoint, it returns a json string. But the string is chopped off to be 16042 characters long, so my json_decode function fails due to invalid format json string.

My question is "Is there a limit for the curl return value ? "

    $ch = curl_init ();
    $timeout = 5;
    curl_setopt ( $ch, CURLOPT_URL, $url );
    curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
    curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );
    curl_setopt ( $ch, CURLOPT_HEADER, true );       
    $output = curl_exec ( $ch );
    $header_size = curl_getinfo ( $ch, CURLINFO_HEADER_SIZE );
    $body = substr ( $output, $header_size );

Or is the substr function chopping it off ?

1

There are 1 best solutions below

1
On
$ch = curl_init ();
$timeout = 60;
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );      
$output = curl_exec ( $ch );
echo $output;

Exaplanation

The CURLOPT_HEADER includes the header in the output. Need to set it to false or remove it. Click here for reference.