Cur*_*tis 11 php curl http-headers
我正在使用PHP的cURL函数从steampowered.com读取配置文件.检索的数据是XML,只需要大约1000个字节.
我正在使用的方法是添加一个Range标头,我在Stack Overflow应答中读到(curl:如何限制GET的大小?).我尝试的另一种方法是使用curlopt_range,但这也不起作用.
<?
$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);
?>
Run Code Online (Sandbox Code Playgroud)
执行此代码时,它返回整个代码.
我使用的是PHP 5.2.14版.
Art*_*cto 18
服务器不支持Range标头.您可以做的最好是在收到的数据超过您想要的时间后立即取消连接.例:
<?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;
Run Code Online (Sandbox Code Playgroud)
也许更干净,你可以使用http包装器(如果编译时也会使用curl --with-curlwrappers).基本上,当您获得的数据超出您的预期时,您将调用fread循环,然后调用fclose流.您还可以使用传输流(如果禁用fsockopen,则使用,而不是fopen手动发送标题打开流)allow_url_fopen.
| 归档时间: |
|
| 查看次数: |
3585 次 |
| 最近记录: |