Control output size Curl

Asked

Viewed 149 times

1

Hello, I have to pull data from a feed using Curl in PHP. Once extracted, I record the result in my bank.

The problem is that the result of the call is giant. Is there any way to control this flow?

This is the code:

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://site.com/?_restfully=true&fromDate=2017-09-07");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");

$headers = array();
$headers[] = "Authorization: Bearer $token";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);

if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
  • I think you can use the CURLOPT_XFERINFOFUNCTION or CURLOPT_WRITEFUNCTION, I will test here and if it works I publish here. If you want to abort the connection if the content is too big you have the CURLOPT_MAXFILESIZE, I think employee.

  • the curl_exec function returns which type?

1 answer

0

PHP, as usual, has no support to the CURLOPT_XFERINFOFUNCTION and not even the CURLOPT_MAXFILESIZE is listed there... One way I see, but not necessarily the best, is to use the CURLOPT_WRITEFUNCTION, as follows:

$result = '';
$size = 0;
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://site.com/?_restfully=true&fromDate=2017-09-07");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $content) use (&$result, $size){
    $result .= $content;
    $len = strlen($content);
    $size += $len;

    if($size > 1000){
        return 0;
    }

    return $len;
});
curl_exec($ch);

if (curl_getinfo($ch, CURLINFO_HTTP_CODE) !== 200) {
    die();
}

echo $result;

This way the result will be limited up to 1000 bytes. Actually this limit is not so rigid, the CURLOPT_WRITEFUNCTION there is no call to each byte received, but yes to each "block" of data, that is there may be 1000 bytes or 500 bytes. For this reason it may be that the result has more than 1000 bytes, but when there are more than 1000 bytes it will stop getting the rest.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.