3

I'm trying all solutions on the full internet and I can't get it.

I'm trying to connect using php curl to an external API sending json data, but I can't set content_type.

$url = "https://example.com:xxxx/api/example";

$data = array(
    'example' => '1',
    'example2' => '2'
);
$jsonData = json_encode($data);

$ch = curl_init($url);

if(extension_loaded('curl') == true){
    echo('Curl is active<br/>');
} else {
    echo('Curl is NOT active<br/>');
}

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type:application/json',
    'Connection: Keep-Alive'
));
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 300); //timeout in seconds
$result = curl_exec($ch);


echo('<hr/>');
var_dump(curl_getinfo($ch));
echo('<br/>');
echo curl_errno($ch) . '<br/>';
echo curl_error($ch) . '<br/>';
echo('<hr/>');


curl_close($ch);

echo('<br/>Response: ');
var_dump($result);

Response:

Curl is active

array(26) { ["url"]=> string(66) "https://example.com:port/api/example" ["content_type"]=> NULL ["http_code"]=> int(0) ["header_size"]=> int(0) ["request_size"]=> int(0) ["filetime"]=> int(-1) ["ssl_verify_result"]=> int(0) ["redirect_count"]=> int(0) ["total_time"]=> float(0.467581) ["namelookup_time"]=> float(0.004158) ["connect_time"]=> float(0) ["pretransfer_time"]=> float(0) ["size_upload"]=> float(0) ["size_download"]=> float(0) ["speed_download"]=> float(0) ["speed_upload"]=> float(0) ["download_content_length"]=> float(-1) ["upload_content_length"]=> float(-1) ["starttransfer_time"]=> float(0) ["redirect_time"]=> float(0) ["redirect_url"]=> string(0) "" ["primary_ip"]=> string(0) "" ["certinfo"]=> array(0) { } ["primary_port"]=> int(0) ["local_ip"]=> string(0) "" ["local_port"]=> int(0) }

7

Failed to connect to https://example.com port xxx: Connection refused

Response: bool(false)

...

And if I try via SoapUi:

HTTP/1.1 200 Vary: Origin Vary: Access-Control-Request-Method Vary: Access-Control-Request-Headers Vary: Origin Vary: Access-Control-Request-Method Vary: Access-Control-Request-Headers X-Content-Type-Options: nosniff X-XSS-Protection: 1; mode=block Cache-Control: no-cache, no-store, max-age=0, must-revalidate Pragma: no-cache Expires: 0 Strict-Transport-Security: max-age=31536000 ; includeSubDomains X-Frame-Options: DENY Content-Type: text/plain;charset=UTF-8 Content-Length: 42 Date: Thu, 11 Jun 2020 09:29:46 GMT Keep-Alive: timeout=60 Connection: keep-alive

correct response

But I need to do it on php. Any help?

Borjawork
  • 31
  • 5
  • curl_getinfo will by default tell you (mostly) info about the **response**, not the request. The [manual](https://www.php.net/manual/en/function.curl-getinfo.php) says of the content-type value: _Content-Type: of the requested document. NULL indicates server did not send valid Content-Type: header_ . So the content-type you're seeing there is the one provided (or not provided, in this case!) by the remote server in its response, not in your request. If you want to see what headers were set in the outgoing request, try this: https://stackoverflow.com/a/24148421/5947043 – ADyson Jun 11 '20 at 09:40
  • Anyway, a "connection refused" error almost certainly has nothing whatsoever to do with your content-type header. The server isn't getting as far as bothering with that, it's just refusing to accept any kind of connection from you at the socket level. More likely you have a firewall issue, or some kind of issue with cURL and SSL/HTTPS. – ADyson Jun 11 '20 at 09:42
  • Thanks you @ADyson . I try to get my Request as the link you share and it returns me NULL. Probably the firewall issue or SSL is the key – Borjawork Jun 11 '20 at 09:49
  • @MarkusZeller can you provide a reference for that requirement? I can't find anything which specifies that. Anyway it's not really relevant to the OP's actual error – ADyson Jun 11 '20 at 10:03
  • @MarkusZeller sure but the syntax guide doesn't actually say you _must_ leave a space. https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers says _" An HTTP header consists of its case-insensitive name followed by a colon (:), then by its value. Whitespace before the value is ignored"_ ...so basically this is saying any whitespace is optional and not taken into account. – ADyson Jun 11 '20 at 10:58
  • @MarkusZeller I don't believe it actually is a rule. The RFC shows examples, but they are just that - examples. the actual syntax guide in each header doesn't appear to require (or forbid) the use of spaces. If you believe it does, please explain why. (And in my experience Mozilla's docs rarely deviate from established standards and conventions either). – ADyson Jun 11 '20 at 11:10
  • @MarkusZeller Sure. But by the time you added that, we'd already established that the content-type header was nothing to do with the error message. People's question titles frequently reflect their assumptions about what's wrong, rather than the real cause. In this case the assumption was based on incorrectly interpreting the data they were seeing on screen. – ADyson Jun 11 '20 at 11:46
  • @MarkusZeller And I just didn't want anyone to go off down a wild goose chase thinking they have to change all their header syntax when I'm 99.9% sure they actually don't. I've just looked in PostMan for example, which is a very well used and respected tool for testing HTTP requests, and if you enter header info as separate header / value fields through the UI, and then click "Bulk Edit", it shows them as text with no spaces near the `:`. And it works fine to send requests, no server I've worked with has ever had trouble reading those headers. – ADyson Jun 11 '20 at 11:47

1 Answers1

0

It is about the https, now I get:

Response: string(34) "xxxxxxxxxxxxxxxxx"

Thanks @ADyson

ADyson
  • 57,178
  • 14
  • 51
  • 63
Borjawork
  • 31
  • 5
  • So what did you change in order to make it work? Please share that for the benefit of others. – ADyson Jun 11 '20 at 10:04
  • Trying to use the same script on a https domain and different server. I used before on domain without https. – Borjawork Jun 11 '20 at 10:16