php + curl问题curl_init上的资源ID#2

时间:2016-06-01 06:11:38

标签: php curl

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';
            }

我做错了什么?

3 个答案:

答案 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';
}