php + curl issue Resource id # 2 on curl

php + curl issue Resource id # 2 on curl_init:

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

What am I doing wrong?


尝试使用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 returns a cURL handle on success, FALSE on errors. So echo $ch; will return something like Resource id #2.

See http://php.net/manual/en/function.curl-init.php

You have to try something like this

$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/48582.html

上一篇: 计算适合于矩形的最大字体大小?

下一篇: php + curl问题curl上的资源ID#2