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?