在下载正文之前处理CURL头 [英] Handle CURL headers before downloading body

查看:119
本文介绍了在下载正文之前处理CURL头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用PHP和CURL(除非有更好的选择,然后CURL在这种情况下),是否可能有一个php函数处理标题响应下载文件之前?

Using PHP and CURL (unless there is a better alternative then CURL in this case), is it possible to have a php function handle the header response before downloading the file?

例如:

我有一个脚本来下载和处理用户提供的网址。我想添加一个检查,以便如果文件对我的进程(不是一个文本文件,太大等)有效,CURL请求将取消之前,服务器浪费时间下载文件。

I have a script that downloads and processes urls supplied by the user. I would like to add a check so that if the file is not valid for my process (not a text file, too large, etc),the CURL request would be cancelled before the server wastes time downloading the file.

更新:解决方案
PEAR类HTTP_Request2: http://pear.php.net/package/HTTP_Request2/
使您能够设置观察者到连接,并抛出异常以随时取消。

Update: Solution PEAR class HTTP_Request2: http://pear.php.net/package/HTTP_Request2/ Gives you the ability to set observers to the connection and throw exceptions to cancel anytime. Works perfectly for my needs!

推荐答案

使用cURL,执行HTTP HEAD请求检查头,然后如果它是有效的状态为200)执行完整的HTTP GET请求。

Using cURL, do a HTTP HEAD request to check the headers, then if it is valid (the status is 200) do the full HTTP GET request.

您必须设置的基本选项是 CURLOPT_NOBODY 将请求更改为HEAD类型

The basic option you must set is CURLOPT_NOBODY, which changes the requested to the type HEAD

curl_setopt($ch, CURLOPT_NOBODY, true);

然后执行查询后,您需要检查返回状态,可以使用 curl_getinfo()

Then after executing the query, you need to check the return status which can be done using curl_getinfo()

$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);

这篇关于在下载正文之前处理CURL头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆