我正在尝试使用curl方法进行API调用。
我已经使用我想要集成的应用程序创建者的演示项目在 MAMP 上尝试了相同的调用,它都是 PHP 代码...现在在服务器上我刚刚包含了 PHP 库并尝试制作在 PHP 中调用,但我不知道为什么我得到状态代码 0。
private function _cURL($url, $data, $request, $headAccept) {
$headers = array($headAccept, "Authorization: Basic " . $this->hash);
$ch = curl_init($url);
// curl_setopt($ch, CURLOPT_MUTE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
if ( !empty($data) ) {
$headers[] = "Content-Type: application/json; charset=utf-8";
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
}
if ( !empty($request)) {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $request);
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// debugging
$isDebug = self::DEBUG_ON_ERROR;
if ( !empty($isDebug) ) {
$debug = array(
'URL: ' => $url,
'data: ' => $data,
'headers: ' => $headAccept,
);
echo '<pre>' , print_r($debug, true), '</pre>';
}
return $ch;
}
我不知道我应该做什么..因为状态只有0..不像403、404等。
谢谢您的帮助。
编辑
$ch = $this->_cURL($url, $data, $request, $headAccept);
$return = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
编辑2 致电
print_r(curl_getinfo($ch));
后,回复是:
(
[url] => https://ws.smartbill.ro:8183/SBORO/api/estimate
[content_type] =>
[http_code] => 0
[header_size] => 0
[request_size] => 0
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0.007306
[namelookup_time] => 0.007255
[connect_time] => 0
[pretransfer_time] => 0
[size_upload] => 0
[size_download] => 0
[speed_download] => 0
[speed_upload] => 0
[download_content_length] => -1
[upload_content_length] => -1
[starttransfer_time] => 0
[redirect_time] => 0
[redirect_url] =>
[primary_ip] =>
[certinfo] => Array
(
)
[primary_port] => 0
[local_ip] =>
[local_port] => 0
)
编辑3
我从生产中下载了该项目,并使用
php artisan:serve
在本地主机上进行了测试,并且有效...所以是服务器问题..但我不知道该怎么办...
您正在尝试使用自定义 TCP 端口 (8183) 调用 URL, 也许您的防火墙不允许此端口?
尝试使用我有的这个功能
public static function get($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 1000);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
$result = curl_exec($ch);
$info = curl_getinfo($ch);
// dd($info);
curl_close($ch);
$result = json_decode($result, true);
return $result;
}
它在我的 laravel 项目上运行得非常好。
看看有没有帮助!