1
votes

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?

3
Please google this and follow the steps. I am sure you will be able to figure it out.Vickrant

3 Answers

5
votes

If you are not on a host with SSL so you should bypass the SSL verification

<?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';
    }
4
votes

Try to use curl_error($ch) and echo to diagnose the error

$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);
3
votes

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