7

If i use the CURLOPT_TCP_FASTOPEN option in my code , then i get the following error.

Use of undefined constant CURLOPT_TCP_FASTOPEN - assumed 'CURLOPT_TCP_FASTOPEN'

The CURLOPT_TCP_FASTOPEN is a supported option in php 7.4.5 interface .

php -v

PHP 7.4.5 (cli) (built: Apr 14 2020 12:54:33) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.5, Copyright (c), by Zend Technologies

curl -V

curl 7.70.0 (x86_64-redhat-linux-gnu) libcurl/7.70.0 NSS/3.44 zlib/1.2.7 libpsl/0.7.0 (+libicu/50.1.2) libssh2/1.9.0 nghttp2/1.31.1
Release-Date: 2020-04-29
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp scp sftp smb smbs smtp smtps telnet tftp
Features: AsynchDNS GSS-API HTTP2 HTTPS-proxy IPv6 Kerberos Largefile libz Metalink NTLM NTLM_WB PSL SPNEGO SSL UnixSockets

What am i doing wrong here ?

Edit 1:

Here are additional info corresponding to YouriKoeman's overview

Kernel version : 3.10.0-1062.12.1.el7.x86_64

OS : unix (Centos 7.x)

curl --tcp-fastopen -L http://www.google.com return the following error:

curl: (55) Send failure: Operation not supported for curl --tcp-fastopen -L http://www.google.com

user2650277
  • 6,289
  • 17
  • 63
  • 132
  • Sorry - just have to check if it's enabled - https://stackoverflow.com/questions/22713591/use-of-undefined-constant-curlopt-post-assumed-curlopt-post – Nigel Ren May 07 '20 at 05:32
  • @NigelRen the curl extension is enable.I can make curl request just fine , only this option is causing issues – user2650277 May 07 '20 at 09:36
  • Could you please share the result of this command: `php -r 'print_r(curl_version()['version']);'` – 0stone0 May 10 '20 at 16:54
  • I am getting `PHP Warning: Use of undefined constant version - assumed 'version' (this will throw an Error in a future version of PHP) in Command line code on line 1` with your command – user2650277 May 10 '20 at 17:50
  • @0stone0 here is a part of phpinfo for curl- https://i.gyazo.com/710f2787f38aa4a6438612fdc9b3ac20.png – user2650277 May 10 '20 at 17:52
  • There should be a version number behind the undefined constant – 0stone0 May 10 '20 at 17:58
  • @0stone0 you were right i missed the full part `PHP Warning: Use of undefined constant version - assumed 'version' (this will throw an Error in a future version of PHP) in Command line code on line 1 7.70.0` – user2650277 May 10 '20 at 18:02
  • Are you by any chance testing the code on windows? it is only supported on unix type operating systems. – YouriKoeman May 12 '20 at 16:07
  • @YouriKoeman i am on centos 7 – user2650277 May 13 '20 at 17:08
  • @user2650277 I have updated my answer with some comments, hope they help – YouriKoeman May 13 '20 at 20:14

2 Answers2

9

I chose to answer in a more broad way to hopefully help more poeple when they encounter issues relating to this and google for answers

(Note: php runtime and Loaded extensions can differ between the CLI and when accessed from a webserver).

What are the system requirements for this feature?

The feature CURLOPT_TCP_FASTOPEN you want to use has some system requirements that have to be met

They are the following:

  1. You must have Kernel version > 3.6 (linux)
  2. You must have PHP 7.0.7 or higher
  3. You must have Curl(program) AND php{your/version}-curl 7.49.0 or higher
  4. You must have a *nix type of operating system (macos, linux, bsd)

how to debug What requirement is not being met?

The fact that the constant is not defined is a red flag that one of these dependencies is not met, but how do i figure out which?

kernel version

This one is easy, run the following command: uname -r.

It must be greater than 3.6

Curl version and build options

The best way to check if the functionality is available in curl is to call curl from the cli with this option, like: curl --tcp-fastopen -O http://google.com

If this request goes successfully, curl is configured correctly on your system, so the problem lies within php

PHP version and extensions

For webserver

use phpinfo() to check if the php version is greater than 7.0.7 And that the php-curl extensions are loaded

For CLI

in the command line type php -v the version should be greater than 7.0.7.

To check the extensions type the following into your command line php -m | grep curl this command should return curl, if nothing is returned the curl extension is not loaded for the php cli.

Community
  • 1
  • 1
YouriKoeman
  • 768
  • 3
  • 10
  • Thanks for the detaiiled answer.I am getting the following error.`curl: (55) Send failure: Operation not supported` for `curl --tcp-fastopen -L http://www.google.com` . There might be something missing in the build options for curl – user2650277 May 13 '20 at 17:19
  • You can install a curl binary with this build option from a custom repo, which one i dont's know for redhat sadly. the option i know for sure that will work is building curl yourself with said build option – YouriKoeman May 13 '20 at 19:50
  • Ps: this might also mean that your kernel version is not up to date (needs to support `send()` networking command). is your kernel version > 3.6 (Above 3.6, not 3.6 itself) – YouriKoeman May 13 '20 at 19:59
  • run the following commands to make sure you are up to date as far as the official (or your custom) repos can provide: `yum -y update kernel` `yum remove curl` `yum install curl` – YouriKoeman May 13 '20 at 20:10
5

The issue was that tcp fast open is not enabled by default until kernel version 3.13.

To enable TCP Fast Open on Centos 7:

1.Add tcp_fastopen in sysctl.d

echo "net.ipv4.tcp_fastopen=3" > /etc/sysctl.d/30-tcp_fastopen.conf 

2.Restart sysctl

systemctl restart systemd-sysctl

3.Verify sysctl setup for tcp_fastopen

cat /proc/sys/net/ipv4/tcp_fastopen should output 3

user2650277
  • 6,289
  • 17
  • 63
  • 132