PHP + curl returns bad request

I have a PHP application which send a request to a site and receive back a XML structure.

This worked without problem some weeks ago but now I receive an error 400 on each request.

I debugged the code via eclipse IDE.
If I copy the PHP generated url ($url) and paste it in the browser it works
But with PHP/curl I receive an error 400 >:(

The php code:

  $ch = curl_init();
  $timeout = 5;
  curl_setopt($ch, CURLOPT_URL, **$url**);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  curl_setopt($ch, CURLOPT_AUTOREFERER, true);
  $userAgent = 'Mozilla/5.0 (X11; Fedora;Linux x86; rv:60.0) Gecko/20100101 Firefox/60.0';
  curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
  
  $xml = curl_exec($ch);
  $msg = curl_getinfo($ch, CURLINFO_HTTP_CODE);

url generated is

http://catalogue.bnf.fr/api/SRU?version=1.2&operation=searchRetrieve&query=(bib.isbn%20all%20"9782877726351")%20and%20(bib.author%20all%20"Coulon")&recordSchema=unimarcxchange

I tried

  • adding the http version via
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1  );

but all versions that I tested give the same error.

  • Removing or changing the user agent but this has none effect.

I think that something changed in PHP or in curl but googling gives only a remark about version ==> default is HTTP/2 with PHP 7.4.

curl and php version

 # curl -V
curl 7.67.0 (x86_64-suse-linux-gnu) libcurl/7.67.0 OpenSSL/1.1.1d zlib/1.2.11 libidn2/2.3.0 libpsl/0.21.0 (+libidn2/2.2.0) libssh/0.9.2/openssl/zlib nghttp2/1.39.2
Release-Date: 2019-11-06
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 IDN IPv6 Kerberos Largefile libz Metalink NTLM NTLM_WB PSL SPNEGO SSL TLS-SRP UnixSockets

# php -v
PHP 7.4.0 (cli) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Xdebug v2.8.0, Copyright (c) 2002-2019, by Derick Rethans

Any idea?
Many thanks in advance
Philippe

Tumbleweed upgraded PHP from 7.3 to 7.4 lately. This is a major upgrade and many php apps will fail. It broke my Nextcloud, for example. Granted, rpm had a warning which I chose to ignore. Had to restore php 7.3 from backup. And since this is the second time this happened, I will probably switch to docker nextcloud. Anyway, try your app with 7.3

Although a version change might work (and it would be a Q&D way of troubleshooting), those commands look very basic to me, so basic I’d be surprised if they were deprecated.

A 400 error suggests either a permissions problem (I’d think unlikely) or a file that is missing or can’t be read… Can the error be traced in a debugger? What IDE are you using if any?

As for deploying and “freezing” a particular subsystem version…
Deploying in a Docker container can be one way.
I’m sure that if a new version of PHP was released just now, you should still be able to find the previous version (7.3) in the openSUSE repositories and lock that version. I’d also ask why anyone would be deploying code on a TW intended for Production use where everything changes rapidly. Deploying on LEAP would slow the pace of versioning immensely.

TSU

Hello;

I forced the reinstall of php7.3.11 from http://download.opensuse.org/history/20191116. But the problem is also present there
I use this curl functionnality quite rarely and it may not have been working for some time. As far as I remember last time that it worked was in August or September.

Regards
Philippe

Hello;

I use the Eclipse IDE. I can debug therein the PHP code and see the different data used. But cannot trace the curl itself. I need to search if a logging can be done for the curl itself.

Regards
Philippe

Did you look up what HTTP return code 400 means? You do realize that this is response from server, do not you? And that the problem has nothing to do with either PHP or curl (at least, the problem is not in the code you show):

bor@bor-Latitude-E5450:~/src/curl$ telnet catalogue.bnf.fr 80
Trying 194.199.8.30...
Connected to catalogue.bnf.fr.
Escape character is '^]'.
GET /api/SRU?version=1.2&operation=searchRetrieve&query=(bib.isbn%20all%20"9782877726351")%20and%20(bib.author%20all%20"Coulon")&recordSchema=unimarcxchange HTTP/1.1


HTTP/1.1 400 Bad Request
Date: Mon, 23 Dec 2019 17:34:50 GMT
Server: Apache
Content-Length: 226
Connection: close
Content-Type: text/html; charset=iso-8859-1


<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>400 Bad Request</title>
</head><body>
<h1>Bad Request</h1>
<p>Your browser sent a request that this server could not understand.<br />
</p>
</body></html>
Connection closed by foreign host.
bor@bor-Latitude-E5450:~/src/curl$ 

url generated is

http://catalogue.bnf.fr/api/SRU?version=1.2&operation=searchRetrieve&query=(bib.isbn%20all%20"9782877726351")%20and%20(bib.author%20all%20"Coulon")&recordSchema=unimarcxchange

This URL is invalid, the " should be percent-encoded too. It is possible that something changed in functions used to generate it. But it most certainly is unrelated to curl at all.

Thanks I corrected this and now it works. I probably did a change using urlencode and forgot the double quote

Many thanks
Philippe