php + curl问题curl_init上的资源ID#2:
$url = "https://example.com:4433/deviceservice/authorize?login=query"; // URL JSON $ch = curl_init($url); echo $ch; //write Resource id # 2 if( $ch ) { curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, TRUE); $json = curl_exec( $ch ); $json = json_decode($json); } else { echo 'nothing'; }
我做错了什么?
答案 0 :(得分:2)
如果您不在使用SSL的主机上,则应绕过SSL验证
<?php
$url = "https://example.com:4433/deviceservice/authorize?login=query";
$ch = curl_init($url);
echo $ch; //write Resource id # 2
if( $ch )
{
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
$json = curl_exec( $ch );
$json = json_decode($json);
} else {
echo 'nothing';
}
答案 1 :(得分:2)
尝试使用curl_error($ ch)和echo来诊断错误
$ch= curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
)
);
$response = curl_exec($ch);
$err_status = curl_error($ch);
echo $err_status;
curl_close($ch);
答案 2 :(得分:1)
curl_init
在成功时返回cURL
句柄,在错误时返回FALSE。
因此echo $ch;
将返回类似资源ID#2的内容。
请参阅http://php.net/manual/en/function.curl-init.php
你必须尝试这样的事情
$url = "https://example.com:4433/deviceservice/authorize?login=query"; // URL JSON
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, TRUE);
$json = curl_exec( $ch );
$json = json_decode($json);
curl_close($ch);
if(empty($json)){
echo 'nothing';
}