相同的代码在两个环境(本地主机和本地开发)中都可以,但是在第三个环境中(产品)则不能。第三是VPS。我必须对要连接的API使用基本身份验证。
这是我的代码:
$curl = curl_init();
//For basic auth
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "SomeUsername:AndItPass");
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
));
$result = curl_exec($curl);
dump($curl);die();
正常运行时dump($curl);die();
的输出(例如在localhost中)为:
curl resource @61 ▼
url: "http://XXXXX:8089/api/usuarios/login?perfil=2&rutPoem=76279543&rut=18863583"
content_type: "application/json; charset=utf-8"
http_code: 200
header_size: 259
request_size: 194
filetime: -1
ssl_verify_result: 0
redirect_count: 0
total_time: 0.022104
namelookup_time: 3.9E-5
connect_time: 0.008342
pretransfer_time: 0.008463
size_upload: 0.0
size_download: 251.0
speed_download: 11355.0
speed_upload: 0.0
download_content_length: 251.0
upload_content_length: -1.0
starttransfer_time: 0.022072
redirect_time: 0.0
redirect_url: ""
primary_ip: "XXXXXXXX"
certinfo: []
primary_port: 8089
local_ip: "192.168.4.108"
local_port: 64594
}
但是在VPS中我得到了:
curl resource @345 ▼
url: "http://XXXXX:8089/api/usuarios/login?perfil=2&rutPoem=76279543&rut=18863583"
content_type: null
http_code: 0
header_size: 0
request_size: 0
filetime: -1
ssl_verify_result: 0
redirect_count: 0
total_time: 0.200986
namelookup_time: 0.000126
connect_time: 0.0
pretransfer_time: 0.0
size_upload: 0.0
size_download: 0.0
speed_download: 0.0
speed_upload: 0.0
download_content_length: -1.0
upload_content_length: -1.0
starttransfer_time: 0.0
redirect_time: 0.0
redirect_url: ""
primary_ip: ""
certinfo: []
primary_port: 0
local_ip: ""
local_port: 0
}
发生了什么事?如果我执行dump(curl_error($curl));
答案 0 :(得分:-1)
如您的问题评论中所述,在我看来,您的IP被阻止了。您可以通过在cURL请求中加载代理进行检查。如果请求未被阻止,则意味着服务器防火墙将您的服务器IP列入了黑名单。您可以要求API所有者解除对IP的阻止,但也要确保首先了解如何将其列入黑名单,以使您的应用符合远程服务器规则。