Getting curl_error(): 2 is not a valid cURL handle resource while fetching all users from freshdesk api
Asked Answered
P

3

37

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.

Pi answered 18/10, 2013 at 6:27 Comment(1)
You can also use if (is_resource($ch)) { curl_close($ch); } to avoid warning.Monmouth
L
104

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

Lilith answered 18/10, 2013 at 6:31 Comment(0)
H
20
echo curl_errno($ch);
echo curl_error($ch);

must be called before curl_close($ch);

Hirschfeld answered 12/10, 2015 at 10:25 Comment(0)
L
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);
Lyrism answered 6/8, 2017 at 4:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.