34

I am creating my own system to managing all tickets which are comes from freshdesk.com through its API. I am making curl request to fetch data from freshdesk.com. With getting data of related to tickers its works fine but when i am requesting for all users through curl request then its give me error:

Warning: curl_errno(): 2 is not a valid cURL handle resource in C:\wamp\www\test.php on line 28.

My code is like that:

$ch = curl_init();  
$cOption = array(
    CURLOPT_URL            => 'http://velocity.freshdesk.com/contacts.xml',
    CURLOPT_HEADER         => 0,
    CURLOPT_USERPWD        => "$email:$password",
    CURLOPT_POST           => false,
    CURLOPT_HTTPHEADER     => array('Content-Type: application/xml'),
    CURLOPT_HTTPAUTH       => CURLAUTH_BASIC,
    CURLOPT_FAILONERROR    => 1,
    CURLOPT_SSL_VERIFYHOST => 2,
    CURLOPT_SSLVERSION     => 2
);  
@curl_setopt_array( $ch, $cOption );  
curl_close($ch);
echo curl_errno($ch);  //line 28
echo curl_error($ch);  //line 29
echo $ch_result;  

Output is:
Warning: curl_errno(): 2 is not a valid cURL handle resource in C:\wamp\www\test.php on line 28.
Warning: curl_errno(): 2 is not a valid cURL handle resource in C:\wamp\www\test.php on line 29.
1 // output of echo $ch_result

Please help me to figure out what is wrong with the code and why this warnings occur.

Michael Sivolobov
  • 12,388
  • 3
  • 43
  • 64
user2393886
  • 832
  • 2
  • 8
  • 17

3 Answers3

98

You use curl_errno and curl_error after closing $ch. It is not right.

You need to close your $ch after fetching information about error.

echo curl_errno($ch);
echo curl_error($ch);
curl_close($ch);

Also you didn't set anything to $ch_result. If you expect that it contains result of your request you are wrong. To fix this you need to add option CURLOPT_RETURNTRANSFER and fetch the result with $ch_result = curl_exec($ch);

davidkonrad
  • 83,997
  • 17
  • 205
  • 265
Michael Sivolobov
  • 12,388
  • 3
  • 43
  • 64
19
echo curl_errno($ch);
echo curl_error($ch);

must be called before curl_close($ch);

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
Won Jun Bae
  • 5,140
  • 6
  • 43
  • 49
12

You use curl_errno and curl_error after closing $ch. It is not right.

You need to close your $ch after fetching information about error.

That's true I get the answer by this .

        $data = curl_exec($ch);
        if (!curl_errno($ch)) {
         ....
        }
        curl_close($ch);
ata
  • 3,398
  • 5
  • 20
  • 31
Milad.biniyaz
  • 496
  • 5
  • 10