0

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?

1
  • Please google this and follow the steps. I am sure you will be able to figure it out. Commented Jun 1, 2016 at 7:01

3 Answers 3

6

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'; } 
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. I've been trying to fix cURL on my WAMP local server. I thought it was not loading at all, I installed plenty of different versions, tried various fixes but nothing worked. Then I realized the curl_init function worked, only the rest didn't. It was the SSL verification issue. You saved me hours.
Saved me, my server now is sending correctly to skebby SMS gateway
4

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); 

Comments

3

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.