php 获取 curl_error(): 2 在从 freshdesk api 获取所有用户时不是有效的 cURL 句柄资源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19442999/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 19:28:16  来源:igfitidea点击:

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

phpcurlticket-system

提问by user2393886

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:

我正在创建自己的系统来管理所有来自 freshdesk.com 通过其 API 的票证。我正在发出 curl 请求以从 freshdesk.com 获取数据。获取与代码相关的数据后,它工作正常,但是当我通过 curl 请求请求所有用户时,它给我错误:

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

警告:curl_errno(): 2 不是第 28 行 C:\wamp\www\test.php 中的有效 cURL 句柄资源。

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

输出是:
警告: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 // echo $ch_result 的输出

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

请帮我弄清楚代码有什么问题以及为什么会出现这种警告。

回答by Michael Sivolobov

You use curl_errnoand curl_errorafter closing $ch. It is not right.

您使用curl_errnocurl_error关闭后$ch。这是不对的。

You need to close your $chafter fetching information about error.

您需要$ch在获取有关错误的信息后关闭您的。

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_RETURNTRANSFERand fetch the result with $ch_result = curl_exec($ch);

你也没有设置任何东西$ch_result。如果您期望它包含您的请求结果,那您就错了。要解决此问题,您需要添加选项CURLOPT_RETURNTRANSFER并获取结果$ch_result = curl_exec($ch);

回答by Won Jun Bae

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

must be called before curl_close($ch);

必须先调用 curl_close($ch);

回答by Milad.biniyaz

You use curl_errnoand curl_errorafter closing $ch. It is not right.

您使用curl_errnocurl_error关闭后$ch。这是不对的。

You need to close your $chafter fetching information about error.

您需要$ch在获取有关错误的信息后关闭您的。

That's true I get the answer by this .

这是真的,我得到了这个答案。

        $data = curl_exec($ch);
        if (!curl_errno($ch)) {
         ....
        }
        curl_close($ch);