Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
限制PHP下载大小有困难';s旋度函数_Php_Curl_Http Headers - Fatal编程技术网

限制PHP下载大小有困难';s旋度函数

限制PHP下载大小有困难';s旋度函数,php,curl,http-headers,Php,Curl,Http Headers,我正在使用PHP的cURL函数从steampowered.com读取配置文件。检索到的数据是XML,只需要大约1000个字节 我使用的方法是添加一个Range头,我在堆栈溢出应答()上读取它。我尝试的另一种方法是使用curlopt_范围,但也不起作用 <? $curl_url = 'http://steamcommunity.com/id/edgen?xml=1'; $curl_handle = curl_init($curl_url); curl_setopt ($curl_handl

我正在使用PHP的cURL函数从steampowered.com读取配置文件。检索到的数据是XML,只需要大约1000个字节

我使用的方法是添加一个Range头,我在堆栈溢出应答()上读取它。我尝试的另一种方法是使用curlopt_范围,但也不起作用

<?
$curl_url = 'http://steamcommunity.com/id/edgen?xml=1';
$curl_handle = curl_init($curl_url);

curl_setopt ($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt ($curl_handle, CURLOPT_HTTPHEADER, array("Range: bytes=0-1000"));

$data_string = curl_exec($curl_handle);

echo $data_string;

curl_close($curl_handle);
?>

当执行此代码时,它将返回全部内容


我使用的是PHP版本5.2.14。

服务器不支持范围标头。您所能做的最好的事情就是在收到的数据超过您想要的数量时立即取消连接。例如:

<?php
$curl_url = 'http://steamcommunity.com/id/edgen?xml=1';
$curl_handle = curl_init($curl_url);

$data_string = "";
function write_function($handle, $data) {
    global $data_string;
    $data_string .= $data;
    if (strlen($data_string) > 1000) {
        return 0;
    }
    else
        return strlen($data);
}

curl_setopt ($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt ($curl_handle, CURLOPT_WRITEFUNCTION, 'write_function');

curl_exec($curl_handle);

echo $data_string;
1000){
返回0;
}
其他的
返回strlen($data);
}
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
curl_setopt($curl_handle,CURLOPT_WRITEFUNCTION,'write_function');
curl\u exec($curl\u handle);
echo$data_字符串;

也许更简洁地说,您可以使用http包装器(如果它是使用
——使用curlwrappers
)编译的,那么也可以使用curl)。基本上,您可以在一个循环中调用
fread
,然后当您获得的数据超过您想要的数据量时,在流中调用
fclose
。如果禁用了
allow\u url\u fopen
,您也可以使用传输流(使用
fsockopen
打开流,而不是
fopen
,并手动发送标题)。

您确定要查询的服务器支持范围吗?因为当我尝试从命令行获取整个文档时,这让我相信steamcommunity.com没有启用该功能这就成功了!尽管如此,我并不完全理解CURLOPT_writef函数的机制。你能解释一下那里发生了什么事吗?再次感谢。@Cur每次收到新数据时,curl扩展都会调用这个回调函数。回调函数接收curl处理程序和刚刚读取的数据。它应该返回读取的字节数,如果没有,它将中止传输(虽然最后一部分没有记录,但似乎是行为)。@Cur OK我在这里找到了文档:“返回实际处理的字节数。如果该数量与传递给函数的数量不同,它将向库发出错误信号。这将中止传输并返回CURLE_WRITE_错误。“谢谢你,这非常有帮助!我会投票支持你的评论和答案,但我还没有足够的代表性:(将数据字符串设置为数值并根据写入大小递增不是更容易吗?例如:
$data_string+=strlen($data);如果($data_string>1000){return 0;}否则{return strlen($data);}
这样,您就不会在某个地方用大量的文本填充变量。