草庐IT

PHP/ curl : inspecting response headers before downloading body

coder 2024-04-06 原文

在 PHP 中使用 Curl,有什么方法可以在下载正文之前检查 HTTP 响应 header 吗? 假设我向某个 URI 发出 GET 请求,并且我只想在 Content-typetext/html 时获取内容。我知道我可以先发出一个 HEAD 请求,然后再决定是否使用 GET,但是是否可以只在一个请求中进行?

我基本上是在寻找与以下 C# 代码等效的代码:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://some.uri");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if(response.ContentType.Equals("text/html")) {
    string body = new StreamReader(response.GetResponseStream()).ReadToEnd();
} else {
    // do nothing
}

这可能吗?

最佳答案

是的,我知道了,在这种情况下你可以使用 CURLOPT_WRITEFUNCTION
为了让事情变得更快,我在我的所有项目中都使用了我自己的 curl 类。
让我也发布一下。
请参阅下面的代码。

<?php
  require_once( "curl.class.php" );

  /** variable to store response body **/
  $response_body  = null;
  /** total size of header in response **/
  $header_size    = null;

  /** initialize the curl object **/
  $curl = new ipCurl( "http://example.com/images/someimage.jpg" );
  /** set a read callback **/
  $curl->setReadCallback( "my_read_callback" );
  /** Include header in response **/
  $curl->includeHeader();
  /** start curl **/
  $curl->createCurl();

  if ( $curl->getError() !== 0 ) {
    /** something went wrong, print the error message, and error code **/
    echo $curl->getError().":".$curl->getErrorMessage();

    if ( $curl->getError() === 23 ) {
      // its not an image file (Failed writing body....)
    }
  }
  else {
    /** Everything fine, lets parse the body from response **/
    $response_body  = substr( $response_body, $header_size );
    file_put_contents( "image.jpg", $response_body );
  }

  /** The function for our curl write callback **/
  function my_read_callback( &$ch, $data ) {
    global $response_body, $header_size;
    /** Get the content type **/
    $content_type = trim( curl_getinfo( $ch, CURLINFO_CONTENT_TYPE ) );
    /** Give it time to get the content-type string from header **/
    if ( !empty( $content_type ) ) {
      /** stop executing curl if its not an image **/
      if ( $content_type !== "image/jpeg" ) {
        return false;
      }
    }
    /** size of the header **/
    $header_size  = curl_getinfo( $ch, CURLINFO_HEADER_SIZE );
    /** append resposne body to $response_body **/
    $response_body  .=  $data;
    /** return current response length **/
    return strlen( $data );
  }
?>

curl.class.php 文件内容

<?php
class ipCurl { 
  private $ch   = null;
  private $url  = null;

  private $_error   = 0;
  private $_errmsg  = null;
  private $_header  = null;
  private $_webpage = null;
  private $_status  = 0;

  public function __construct( $url = null ) {
    $this->url  = $url;

    if ( !function_exists( "curl_init" ) ) {
      throw new Exception( "Fatal Error: Module 'Curl' is not installed properly" );
    }

    $this->ch = curl_init();

        curl_setopt( $this->ch, CURLOPT_RETURNTRANSFER, true );
        curl_setopt( $this->ch, CURLOPT_FRESH_CONNECT, false );
        curl_setopt( $this->ch, CURLOPT_FORBID_REUSE, false );

    $this->setTimout( 40 );
    $this->setConnTimout( 30 );
    $this->followLocation();
    $this->setMaxRedirects( 4 );
    $this->excludeHeader();
    $this->includeBody();
    $this->verifySSL();
    $this->setBinaryTransfer();
    $this->setReferer( $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"] );
    $this->setUserAgent();

    return $this;
  }

  public function __destruct() {
    curl_close( $this->ch );
    $this->ch = null;
    }

  public function setReadCallback( $callback = null ) {
    curl_setopt( $this->ch, CURLOPT_WRITEFUNCTION, $callback );
    return $this;
  }

  public function setProgressCallback( $callback = null, $buffer = 128 ) {
    curl_setopt( $this->ch, CURLOPT_NOPROGRESS, false );
    curl_setopt( $this->ch, CURLOPT_PROGRESSFUNCTION, $callback );
    curl_setopt( $this->ch, CURLOPT_BUFFERSIZE, $buffer );
    return $this;
  }

  public function includeHeader() {
    curl_setopt( $this->ch, CURLOPT_HEADER, true );
    return $this;
  }
  public function excludeHeader() {
    curl_setopt( $this->ch, CURLOPT_HEADER, false );
    return $this;
  }

  public function includeBody() {
    curl_setopt( $this->ch, CURLOPT_NOBODY, false );
    return $this;
  }
  public function excludeBody() {
    curl_setopt( $this->ch, CURLOPT_NOBODY, true );
    return $this;
  }

  public function setMaxRedirects( $redirects = 4 ) {
    if ( $this->is_safe_mode() ) {
      return $this;
    }
    curl_setopt( $this->ch, CURLOPT_MAXREDIRS, $redirects );
    return $this;
  }

  public function followLocation() {
    if ( $this->is_safe_mode() ) {
      return $this->unfollowLocation();
    }
    curl_setopt( $this->ch, CURLOPT_FOLLOWLOCATION, true );
    return $this;
  }
  public function unfollowLocation() {
    curl_setopt( $this->ch, CURLOPT_FOLLOWLOCATION, false );
    return $this;
  }

  public function setReferer( $referer = null ) {
    curl_setopt( $this->ch, CURLOPT_REFERER, $referer );
    return $this;
  }

  public function setBinaryTransfer( $binary = false ) {
    curl_setopt( $this->ch, CURLOPT_BINARYTRANSFER, $binary );
    return $this;
  }

  public function setTimout( $timeout ) {
    curl_setopt( $this->ch, CURLOPT_TIMEOUT, $timeout );
    return $this;
  }

  public function setConnTimout( $timeout ) {
    curl_setopt( $this->ch, CURLOPT_CONNECTTIMEOUT, $timeout );
    return $this;
  }

  public function setUserAgent( $userAgent = null ) {
    $userAgent  = ( !$userAgent ) ? "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31" : $userAgent;
    curl_setopt( $this->ch, CURLOPT_USERAGENT, $userAgent );
    return $this;
  }

  public function setProxy( $url = null, $port = 0, $username = null, $password = null ) {
    curl_setopt( $this->ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC );
    curl_setopt( $this->ch, CURLOPT_PROXY, $url.( ( $port ) > 0 ? ":".$port : null ) );

    if ( $port > 0 ) {
      curl_setopt( $this->ch, CURLOPT_PROXYPORT, $port );
    }

    if ( $username ) {
      curl_setopt( $this->ch, CURLOPT_PROXYUSERPWD, $username.":".$password );
    }

    return $this;
    }

  public function setAuth( $username = null, $password = null ) {
    curl_setopt( $this->ch, CURLOPT_USERPWD, $username.':'.$password );
    return $this;
    }

  public function setCookiFile( $file = "cookie.txt" ) {
    if ( !$file ) {
      return $this;
    }
    curl_setopt( $this->ch, CURLOPT_COOKIEJAR, $file );
    curl_setopt( $this->ch, CURLOPT_COOKIEFILE, $file );
    return $this;
  }

  public function verifySSL( $ssl = false ) {
    if ( !$ssl ) {
      curl_setopt( $this->ch, CURLOPT_SSL_VERIFYPEER, false );
      curl_setopt( $this->ch, CURLOPT_SSL_VERIFYHOST, 2 );
    }
    else {
      curl_setopt( $this->ch, CURLOPT_SSL_VERIFYPEER, true );
    }
    return $this;
  }

  public function setPost( $postFields = null, $keep_array = false ) {
    if ( is_array( $postFields ) && !$keep_array ) {
      $postFields = http_build_query( $postFields );
    }

    curl_setopt( $this->ch, CURLOPT_POST, true );
    curl_setopt( $this->ch, CURLOPT_POSTFIELDS, $postFields );

    return $this;
  }

  public function setFile( $file = null ) {
    if ( $file !== null ) {
      $file = realpath( $file );
    }
    if ( $file && is_readable( $file ) ) {
      $fp = fopen( $file, "w" );
      curl_setopt( $this->ch, CURLOPT_FILE, $fp );
    }

    return $this;
  }

  public function setHeader( $header = array( "Expect:" ) ) {
    curl_setopt( $this->ch, CURLOPT_HTTPHEADER, $header );
    return $this;
  }

  public function createCurl( $url = null ) {
    $url  = ( $url ) ? trim( $url ) : trim( $this->url );

    if ( !$url ) {
      throw new Exception( "Fatal Error: you must provide a valid url before calling 'createCurl'" );
    }
    curl_setopt( $this->ch, CURLOPT_URL, $url );

    $this->_webpage = curl_exec( $this->ch );
    $this->_status  = (int)curl_getinfo( $this->ch, CURLINFO_HTTP_CODE );
    $this->_error   = (int)curl_errno( $this->ch );
    $this->_errmsg  = curl_error( $this->ch );
    $this->_header  = curl_getinfo( $this->ch );

    if ( !$this->_errmsg ) {
      $this->_errmsg  = $this->parse_http_code( $this->_status );
    }

    return $this;
  }

  private function parse_http_code( $code = 404 ) {
    $code = (int)$code;
    if ( !class_exists( "ipStatusCodes" ) ) {
      return null;
    }
    return ipStatusCodes::info( $code );
  }

  private function is_safe_mode() {
    return ( @ini_get( 'open_basedir' ) != '' && @ini_get( 'safe_mode' ) != 'Off' );
  }

  public function getStatus() {
    return $this->_status;
  }

  public function getResponse() {
    return $this->_webpage;
  }

  public function getHeader() {
    return $this->_header;
  }

  public function getError() {
    return $this->_error;
  }

  public function getErrorMessage() {
    return $this->_errmsg;
  }
}
?>

我不是强制你使用我的类(class),你也可以像这样简单地使用它......

<?php
  curl_setopt( $ch, CURLOPT_WRITEFUNCTION, "my_callback" );
  function my_callback() {
    // same like the "my_read_callback" function in my above code
  }
?>

关于PHP/ curl : inspecting response headers before downloading body,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17390319/

有关PHP/ curl : inspecting response headers before downloading body的更多相关文章

  1. ruby - 在 Ubuntu 14.04 中使用 Curl 安装 RVM 时出错 - 2

    我试图在Ubuntu14.04中使用Curl安装RVM。我运行了以下命令:\curl-sSLhttps://get.rvm.io|bash-sstable出现如下错误:curl:(7)Failedtoconnecttoget.rvm.ioport80:Networkisunreachable非常感谢解决此问题的任何帮助。谢谢 最佳答案 在执行curl之前尝试这个:echoipv4>>~/.curlrc 关于ruby-在Ubuntu14.04中使用Curl安装RVM时出错,我们在Stack

  2. ruby-on-rails - 这个 C 和 PHP 程序员如何学习 Ruby 和 Rails? - 2

    按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visitthehelpcenter指导。关闭9年前。我来自C、php和bash背景,很容易学习,因为它们都有相同的C结构,我可以将其与我已经知道的联系起来。然后2年前我学了Python并且学得很好,Python对我来说比Ruby更容易学。然后从去年开始,我一直在尝试学习Ruby,然后是Rails,我承认,直到现在我还是学不会,讽刺的是那些打着简单易学的烙印,但是对于我这样一个老练的程序员来说,我只是无法将它

  3. ruby - ruby 中等效的 curl 命令 - 2

    我有一个运行良好的curl命令,但我需要在ruby​​脚本中自动执行它,curl命令:curl-uusrname:pwd-XPOST--data"del=false&val=100"http://localhost:1111/sample/path我写了下面的代码:uri=URI::HTTPS.build(:host=>"localhost",:port=>1111)uri.path=URI.escape("/sample/path")client=Net::HTTP.new("localhost","1111")req=Net::HTTP::Post.new(uri.request_

  4. ruby - ruby 中的 400 个错误请求但不是 curl - 2

    我正在尝试在ruby​​中发出https发布请求,它在curl中工作,但我在ruby​​中收到400错误请求,我无法弄清楚原因。这是ruby代码:require'rubygems'require'net/https'require'uri'require'json'uri=URI.parse("https://api.parse.com/1/push")http=Net::HTTP.new(uri.host,uri.port)http.use_ssl=truehttp.verify_mode=OpenSSL::SSL::VERIFY_NONEreq=Net::HTTP::Post.ne

  5. ruby-on-rails - Rails 还是 Sinatra? PHP程序员入门学习哪个好? - 2

    按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visitthehelpcenter指导。关闭10年前。我使用PHP的时间太长了,对它感到厌倦了。我也想学习一门新语言。我一直在使用Ruby并且喜欢它。我必须在Rails和Sinatra之间做出选择,那么您会推荐哪一个?Sinatra真的不能用来构建复杂的应用程序,它只能用于简单的应用程序吗?

  6. ruby - Ruby 对 curl 进行系统调用的正确语法是什么? - 2

    要刷新Redmine,我需要SVN从我们的提交后Hookping我们的Redmine安装。我们的提交后Hook是一个生成电子邮件的Ruby脚本。我想插入一个调用:curl--insecurehttps://redmineserver+webappkey此调用在命令行中有效,但当我尝试这样做时:#!/usr/bin/ruby-wREFRESH_DRADIS_URL="https://redmineserver+webappkey"system("/usr/bin/curl","--insecure","#{REFRESH_DRADIS_URL}")这是行不通的。我如何在ruby中做到这一

  7. ruby-on-rails - PHP 魔术方法 __call、__get 和 __set 的 Ruby 等价物 - 2

    我很确定Ruby有这些(等同于__call、__get和__set),否则find_by将如何在Rails中工作?也许有人可以举一个简单的例子来说明如何定义与find_by相同的方法?谢谢 最佳答案 简而言之你可以映射__调用带有参数的method_missing调用__设置为方法名称以'='结尾的method_missing调用__获取不带任何参数的method_missing调用__调用PHPclassMethodTest{publicfunction__call($name,$arguments){echo"Callingob

  8. ruby - Lisp - 是否适合网络编程/应用程序(交互式)? ruby 的方式是? php的方式是? - 2

    Lisp是否适合Web编程/应用程序(交互式),就像ruby​​和php一样?需要考虑的事情是:易于使用可部署性难度(尤其是对于编程初学者而言)(编辑)在阅读PaulGraham'sessay之后,我特别提到了CommonLisp.将是我的第一门编程语言。在这方面。这样做合适吗?我听说Clojure的宏功能不如CommonLisp的强大,这就是我尝试学习Clojure的原因。它教授编程并且非常强大。 最佳答案 Lisp是一个语系,而不是单一的语言。为了稍微回答您的问题,是的,存在用于各种Lisp方言的Web框架,例如用于Common

  9. 软件工程毕业设计课题(81)微信小程序毕业设计PHP校园跑腿小程序系统设计与实现 - 2

        项目背景和意义 目的:本课题主要目标是设计并能够实现一个基于微信校园跑腿小程序系统,前台用户使用小程序发布跑腿任何和接跑腿任务,后台管理使用基于PHP+MySql的B/S架构;通过后台管理跑腿的用户、查看跑腿信息和对应订单。意义:手机网络时代,大学生通过手机网购日常用品、外卖外卖、代取快递等已不再是稀奇的事情。此外,不少高校还流行着校园有偿工作,校园跑腿就成了大学生创业服务项目。        因为你在校园里,所以不会有进入的限制。并不是所有的外卖平台都可以随意进入校园,比如小黄和小蓝的双打外卖平台。许多大学禁止送餐进入学校,更不用说送餐进入宿舍了。这一措施使得校园服务市场的竞争相对不

  10. K8s部署PHP项目 - 2

    前言    前端时间PHP项目部署升级需要,需要把Laravel开发的项目部署K8s上,下面以laravel项目为例,讲解采用yaml文件方式部署项目。一、部署步骤1.创建Dockerfile文件Dockerfile是一个用来构建镜像的文本文件,在容器运行时,需要把项目文件和项目运行所必须的组件安装其中。#基础镜像FROMphp:7.4-fpm#时区ARGTZ=Asia/Shanghai#更换容器时区RUNcp"/usr/share/zoneinfo/$TZ"/etc/localtime&&echo"$TZ">/etc/timezone#替换成阿里apt-get源RUNsed-i"s@http

随机推荐