问题
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.
回答1:
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);
回答2:
echo curl_errno($ch);
echo curl_error($ch);
must be called before curl_close($ch);
回答3:
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.
thats true i get answer this .
$data = curl_exec($ch);
if (!curl_errno($ch)) {
....
}
curl_close($ch);
来源:https://stackoverflow.com/questions/19442999/getting-curl-error-2-is-not-a-valid-curl-handle-resource-while-fetching-all-u