Home » Php » php – Getting curl_error(): 2 is not a valid cURL handle resource while fetching all users from freshdesk api

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

Posted by: admin April 23, 2020 Leave a comment

Questions:

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.

How to&Answers:

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

Answer:

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

must be called before curl_close($ch);

Answer:

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