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

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

我究竟做错了什么?


尝试使用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);

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

如果您不在具有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';
    }
链接地址: http://www.djcxy.com/p/48581.html

上一篇: php + curl issue Resource id # 2 on curl

下一篇: How to load details automatically