Curl_exec 在 php 中返回 null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29203753/
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-26 00:18:14  来源:igfitidea点击:

Curl_exec return null in php

phpcurl

提问by MuteX

I have a problem to get the data using curl operation. Here i hide the token, If i use the url only in my browser then it returns the data but here its null.

我在使用 curl 操作获取数据时遇到问题。在这里,我隐藏了令牌,如果我仅在浏览器中使用该 url,则它会返回数据,但此处为 null。

<?php 
$token = "TOKEN"; //the actual token hidden
$url = "https://crm.zoho.com/crm/private/xml/Leads/getRecords?authtoken=".$token."&scope=crmapi";
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);

$result = curl_exec($ch);
curl_close($ch);
echo $result; //does not return anything
?>

Where i do mistake please help me.

我做错的地方请帮助我。

回答by jogesh_pi

This is how you can try with CURLOPT_RETURNTRANSFERwhich is used to return the output and curl_errno()to track the errors :

这是您可以尝试使用CURLOPT_RETURNTRANSFERwhich 用于返回输出和curl_errno()跟踪错误的方法:

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, "http://example.com/my_url.php" ); 
curl_setopt($ch, CURLOPT_POST, 1 ); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
$postResult = curl_exec($ch); 

if (curl_errno($ch)) { 
   print curl_error($ch); 
} 
curl_close($ch); 

Helpful Links: curl_errno(), curl_error()

有用的链接:curl_errno()curl_error()

回答by Ayyanar G

Try adding these lines:

尝试添加这些行:

<?php 
$token = "TOKEN"; //the actual token hidden
$url = "https://crm.zoho.com/crm/private/xml/Leads/getRecords";
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
    curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array("authtoken"=>$token,"scope"=>"crmapi"));
$result = curl_exec($ch);
curl_close($ch);
echo $result; //does not return anything
?>

回答by Jeff Wang

please see below article , not just turn off verify , you should update your php.ini with pem file

请看下面的文章,不要只是关闭验证,你应该用 pem 文件更新你的 php.ini

https://snippets.webaware.com.au/howto/stop-turning-off-curlopt_ssl_verifypeer-and-fix-your-php-config/

https://snippets.webaware.com.au/howto/stop-turning-off-curlopt_ssl_verifypeer-and-fix-your-php-config/

Anyone running a recent Linux distribution is probably already OK, because they get the curl libraries bundled in through their package managers and recent curl libraries come with the latest CA root certificate bundle from Mozilla.org.

任何运行最新 Linux 发行版的人都可能已经可以了,因为他们通过包管理器捆绑了 curl 库,并且最近的 curl 库带有来自 Mozilla.org 的最新 CA 根证书包。

For a PHP installation that doesn't come with this file, like the Windows PHP distribution, you need to download the CA root certificate bundle and tell PHP where to find it.

对于不附带此文件的 PHP 安装,如 Windows PHP 发行版,您需要下载 CA 根证书包并告诉 PHP 在哪里可以找到它。