Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #17934

Re: How to handle curl_exec() so that it won't terminat my script abruptly without any exception?

From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.php
Subject Re: How to handle curl_exec() so that it won't terminat my script abruptly without any exception?
Date 2019-06-05 21:35 +0200
Organization PointedEars Software (PES)
Message-ID <5281068.DvuYhMxLoT@PointedEars.de> (permalink)
References <1o3z8vwk6x9am$.54ia46ki18ga$.dlg@40tude.net>

Show all headers | View raw


JJ wrote:

> I'm trying to make a simple proxy script for my web server in my local
> network, but I've found that `curl_exec()` abruptly terminates my script
> without any error or exception on some websites.
                                 ^^^^^^^^^^^^^^^^ 
> FYI, my PHP application came from XAMPP
> (xampp-portable-win32-7.3.2-0-VC15).
> 
> Here's the simplified script to reproduce the problem.
> 
>   <?php
>   function doResponseHeaders($cu, $dt) {
>     header($dt);
>     return strlen($dt);
>   }
>   $cu = curl_init('https://www.youtube.com/');
>   curl_setopt($cu, CURLOPT_HEADERFUNCTION, 'doResponseHeaders');
>   try {
>     echo 'CURL = ' . curl_exec($cu);
>   } catch (Exception $e) {
>     echo 'CURL_EXCEPTION';
>   }
>   curl_close($cu);
>   echo 'ALL_DONE';
>   ?>
> 
> When run, the HTTP 200 is returned, but there is no HTTP body.

At first I thought it would be the usual: YouTube would not take kindly to 
being accessed by non-browsers.  But:

| $ curl --user-agent 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36
| (KHTML, like Gecko) Chrome/73.0.3683.75 Safari/537.36'
| http://www.youtube.com/
| $

  [empty]

The reason why this "does not work" “with some Web sites” is very simple:

| $ HEAD -USe http://www.youtube.com/
| HEAD http://www.youtube.com/
| User-Agent: lwp-request/6.15 libwww-perl/6.15
| 
| 301 Moved Permanently
  ^^^^^^^^^^^^^^^^^^^^^
| Cache-Control: no-cache
| Connection: close
| Date: Wed, 05 Jun 2019 19:20:54 GMT
| Location: https://www.youtube.com/
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| Server: YouTube Frontend Proxy
| Content-Length: 0
| […]

The insecure access is being redirected to the secure Web site, and your 
curl_exec() call does not handle that.  Without following the redirection, 
the HTTP body actually *is* empty.
 
> I did found out that the problem doesn't occur if I don't let the
> `Transfer-Encoding` HTTP response header be processed by `header()`.
> However, the main problem is the fact that `curl_exec()` terminates the
> script abruptly without any exception.
> And **any** code after `curl_exec()` is not executed.

Unlikely.

> So, how to handle this kind of error so that it won't terminate the
> script?

You can set CURLOPT_FOLLOWLOCATION to TRUE (and fine-tune e.g. with 
CURLOPT_MAXREDIRS), and you should probably do that (so that it works even 
if YouTube etc. changes their mind again; but the simplest solution is to 
use 'https://www.youtube.com/' etc. in the first place.

Next time, look at the response status code first.

<https://php.net/curl>

Your From is borked.

-- 
PointedEars
Zend Certified PHP Engineer <http://www.zend.com/en/yellow-pages/ZEND024953>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.

Back to comp.lang.php | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

How to handle curl_exec() so that it won't terminat my script abruptly without any exception? JJ <jj4public@vfemail.net> - 2019-06-05 10:45 +0700
  Re: How to handle curl_exec() so that it won't terminat my script abruptly without any exception? "J.O. Aho" <user@example.net> - 2019-06-05 06:54 +0200
    Re: How to handle curl_exec() so that it won't terminat my script abruptly without any exception? JJ <jj4public@vfemail.net> - 2019-06-06 18:55 +0700
      Re: How to handle curl_exec() so that it won't terminat my script abruptly without any exception? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2019-06-06 18:52 +0200
        Re: How to handle curl_exec() so that it won't terminat my script abruptly without any exception? JJ <jj4public@vfemail.net> - 2019-06-07 19:10 +0700
          Re: How to handle curl_exec() so that it won't terminat my script abruptly without any exception? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2019-06-07 15:26 +0200
            Re: How to handle curl_exec() so that it won't terminat my script abruptly without any exception? JJ <jj4public@vfemail.net> - 2019-06-08 13:15 +0700
              Re: How to handle curl_exec() so that it won't terminat my script abruptly without any exception? JJ <jj4public@vfemail.net> - 2019-06-08 13:35 +0700
                Re: How to handle curl_exec() so that it won't terminat my script abruptly without any exception? Jerry Stuckle <jstucklex@attglobal.net> - 2019-06-08 11:58 -0400
                Re: How to handle curl_exec() so that it won't terminat my script abruptly without any exception? JJ <jj4public@vfemail.net> - 2019-06-09 08:46 +0700
                Re: How to handle curl_exec() so that it won't terminat my script abruptly without any exception? Jerry Stuckle <jstucklex@attglobal.net> - 2019-06-09 22:10 -0400
                Re: How to handle curl_exec() so that it won't terminat my script abruptly without any exception? Jerry Stuckle <jstucklex@attglobal.net> - 2019-06-09 22:13 -0400
                Re: How to handle curl_exec() so that it won't terminat my script abruptly without any exception? JJ <jj4public@vfemail.net> - 2019-06-10 20:42 +0700
                Re: How to handle curl_exec() so that it won't terminat my script abruptly without any exception? Jerry Stuckle <jstucklex@attglobal.net> - 2019-06-11 13:15 -0400
                Re: How to handle curl_exec() so that it won't terminat my script abruptly without any exception? JJ <jj4public@vfemail.net> - 2019-06-12 05:51 +0700
                Re: How to handle curl_exec() so that it won't terminat my script abruptly without any exception? Jerry Stuckle <jstucklex@attglobal.net> - 2019-06-12 10:37 -0400
                Re: How to handle curl_exec() so that it won't terminat my script abruptly without any exception? JJ <jj4public@vfemail.net> - 2019-06-12 22:11 +0700
                Re: How to handle curl_exec() so that it won't terminat my script abruptly without any exception? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2019-06-11 18:24 +0200
              Re: How to handle curl_exec() so that it won't terminat my script abruptly without any exception? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2019-06-08 13:20 +0200
                Re: How to handle curl_exec() so that it won't terminat my script abruptly without any exception? JJ <jj4public@vfemail.net> - 2019-06-09 09:08 +0700
                Re: How to handle curl_exec() so that it won't terminat my script abruptly without any exception? Jerry Stuckle <jstucklex@attglobal.net> - 2019-06-09 22:12 -0400
                Re: How to handle curl_exec() so that it won't terminat my script abruptly without any exception? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2019-06-11 18:05 +0200
                Re: How to handle curl_exec() so that it won't terminat my script abruptly without any exception? Luuk <luuk@invalid.lan> - 2019-06-11 20:19 +0200
            Re: How to handle curl_exec() so that it won't terminat my script abruptly without any exception? JJ <jj4public@vfemail.net> - 2019-06-08 13:19 +0700
              Re: How to handle curl_exec() so that it won't terminat my script abruptly without any exception? JJ <jj4public@vfemail.net> - 2019-06-08 13:37 +0700
              Re: How to handle curl_exec() so that it won't terminat my script abruptly without any exception? Luuk <luuk@invalid.lan> - 2019-06-09 09:41 +0200
                Re: How to handle curl_exec() so that it won't terminat my script abruptly without any exception? JJ <jj4public@vfemail.net> - 2019-06-10 20:32 +0700
                Re: How to handle curl_exec() so that it won't terminat my script abruptly without any exception? Luuk <luuk@invalid.lan> - 2019-06-10 16:34 +0200
                Re: How to handle curl_exec() so that it won't terminat my script abruptly without any exception? Luuk <luuk@invalid.lan> - 2019-06-10 16:38 +0200
                Re: How to handle curl_exec() so that it won't terminat my script abruptly without any exception? Luuk <luuk@invalid.lan> - 2019-06-10 16:57 +0200
                Re: How to handle curl_exec() so that it won't terminat my script abruptly without any exception? JJ <jj4public@vfemail.net> - 2019-06-11 12:42 +0700
                Re: How to handle curl_exec() so that it won't terminat my script abruptly without any exception? Luuk <luuk@invalid.lan> - 2019-06-11 08:18 +0200
                Re: How to handle curl_exec() so that it won't terminat my script abruptly without any exception? JJ <jj4public@vfemail.net> - 2019-06-12 05:51 +0700
  Re: How to handle curl_exec() so that it won't terminat my script abruptly without any exception? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2019-06-05 21:35 +0200
  Re: How to handle curl_exec() so that it won't terminat my script abruptly without any exception? Arno Welzel <usenet@arnowelzel.de> - 2019-06-12 08:54 +0200
    Re: How to handle curl_exec() so that it won't terminat my script abruptly without any exception? JJ <jj4public@vfemail.net> - 2019-06-12 18:07 +0700
      Re: How to handle curl_exec() so that it won't terminat my script abruptly without any exception? JJ <jj4public@vfemail.net> - 2019-06-12 21:51 +0700
        Re: How to handle curl_exec() so that it won't terminat my script abruptly without any exception? Arno Welzel <usenet@arnowelzel.de> - 2019-06-12 17:12 +0200
          Re: How to handle curl_exec() so that it won't terminat my script abruptly without any exception? JJ <jj4public@vfemail.net> - 2019-06-13 00:12 +0700
      Re: How to handle curl_exec() so that it won't terminat my script abruptly without any exception? Arno Welzel <usenet@arnowelzel.de> - 2019-06-12 16:59 +0200
    Re: How to handle curl_exec() so that it won't terminat my script abruptly without any exception? Luuk <luuk@invalid.lan> - 2019-06-12 22:25 +0200

csiph-web