简体   繁体   English

PHP cURL 内容类型未设置

[英]PHP cURL Content-Type not set

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.我正在尝试使用 php curl 连接到外部 API 发送 Z466DEEC76ECDF5FCA6D38571F63

$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 Curl 处于活动状态

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) } array(26) { ["url"]=> string(66) " https://example.com:port/api/example " ["content_type"]=> Z6C3E226B4D4795D518AB341B08">inEC226B4D4795D518AB341B08"= ["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 7

Failed to connect to https://example.com port xxx: Connection refused无法连接到https://example.com端口 xxx:连接被拒绝

Response: bool(false)响应:布尔(假)

... ...

And if I try via SoapUi:如果我通过 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; 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-类型选项: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; 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 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-活

correct response正确的反应

But I need to do it on php.但我需要在 php 上执行此操作。 Any help?有什么帮助吗?

It is about the https, now I get:它是关于 https,现在我得到:

Response: string(34) "xxxxxxxxxxxxxxxxx"

Thanks @ADyson谢谢@ADyson

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM