1
votes

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?

1
curl_getinfo will by default tell you (mostly) info about the response, not the request. The manual 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: stackoverflow.com/a/24148421/5947043 - ADyson
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
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
@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
@MarkusZeller sure but the syntax guide doesn't actually say you must leave a space. 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

1 Answers

0
votes

It is about the https, now I get:

Response: string(34) "xxxxxxxxxxxxxxxxx"

Thanks @ADyson