Use of undefined constant CURLOPT_TCP_FASTOPEN
Asked Answered
E

2

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

Ethics answered 7/5, 2020 at 5:27 Comment(10)
Sorry - just have to check if it's enabled - #22714091Coleridge
@NigelRen the curl extension is enable.I can make curl request just fine , only this option is causing issuesEthics
Could you please share the result of this command: php -r 'print_r(curl_version()['version']);'Sympathin
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 commandEthics
@Sympathin here is a part of phpinfo for curl- i.gyazo.com/710f2787f38aa4a6438612fdc9b3ac20.pngEthics
There should be a version number behind the undefined constantSympathin
@Sympathin 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.0Ethics
Are you by any chance testing the code on windows? it is only supported on unix type operating systems.Rashida
@YouriKoeman i am on centos 7Ethics
@Ethics I have updated my answer with some comments, hope they helpRashida
E
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

Ethics answered 14/5, 2020 at 6:41 Comment(1)
I didn't know it was off by default (i run a rolling release distro). Good find! glad it's fixedRashida
R
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.

Rashida answered 12/5, 2020 at 16:19 Comment(4)
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 curlEthics
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 optionRashida
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)Rashida
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 curlRashida
E
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

Ethics answered 14/5, 2020 at 6:41 Comment(1)
I didn't know it was off by default (i run a rolling release distro). Good find! glad it's fixedRashida

© 2022 - 2024 — McMap. All rights reserved.