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

偶尔善良 提交于 2019-12-04 09:57:17

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!