简体   繁体   English

PHP Curl 已启用但不起作用

[英]PHP Curl is enabled but not working

I'm trying to get data from an API using PHP CURL extension but is not working.我正在尝试使用 PHP CURL 扩展从 API 获取数据,但无法正常工作。 The same code in another server is running.另一台服务器中的相同代码正在运行。

(Server 1) CURL configuration: It works (服务器 1)CURL 配置:它有效

(Server 2) CURL configuration: Does not work (服务器 2)CURL 配置:不起作用

Is any firewall configuration?有没有防火墙配置?

<?php

    $con = curl_init();
    $url = "https://api.moloni.com/v1/companies/getOne/?access_token=123456";
    $my_values = array('company_id' => 0);

    curl_setopt($con, CURLOPT_URL, $url);
    curl_setopt($con, CURLOPT_POST, true);
    curl_setopt($con, CURLOPT_POSTFIELDS, http_build_query($my_values));
    curl_setopt($con, CURLOPT_HEADER, false);
    curl_setopt($con, CURLOPT_RETURNTRANSFER, true);

    $res_curl = curl_exec($con);
    curl_close($con);

    $res_txt = json_decode($res_curl, true);
    if(!isset($res_txt['error'])){
        echo 'Result: '.print_r($res_txt,true).'';
    }else{
        echo 'Erro: '.print_r($res_txt,true).'';
    }

    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);

?>

Edit:编辑:

<?php

    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);


    $con = curl_init();
    $url = "https://api.moloni.com/v1/companies/getOne/?access_token=123456";
    $my_values = array('company_id' => 0);

    curl_setopt($con, CURLOPT_URL, $url);
    curl_setopt($con, CURLOPT_POST, true);
    curl_setopt($con, CURLOPT_POSTFIELDS, http_build_query($my_values));
    curl_setopt($con, CURLOPT_HEADER, false);
    curl_setopt($con, CURLOPT_RETURNTRANSFER, true);

    $res_curl = curl_exec($con);

    if($res_curl === false){ echo 'Curl error: ' . curl_error($con);}

    curl_close($con);


    $res_txt = json_decode($res_curl, true);
    if(!isset($res_txt['error'])){
        echo 'Result: '.print_r($res_txt,true).'';
    }else{
        echo 'Erro: '.print_r($res_txt,true).'';
    }


?>

Result:结果:

Server 1: Erro: Array ( [error] => invalid_request [error_description] => Invalid access token. ) It works!服务器 1: Erro: Array ( [error] => invalid_request [error_description] => Invalid access token. ) 它有效!

Server 2: Curl error: SSL certificate problem: unable to get local issuer certificate服务器 2: Curl 错误:SSL 证书问题:无法获取本地颁发者证书

Edit 2:编辑2:

<?php

    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);


    $con = curl_init();
    $url = "https://api.moloni.com/v1/companies/getOne/?access_token=123456";
    $my_values = array('company_id' => 0);

    curl_setopt($con, CURLOPT_URL, $url);
    curl_setopt($con, CURLOPT_POST, true);
    curl_setopt($con, CURLOPT_POSTFIELDS, http_build_query($my_values));
    curl_setopt($con, CURLOPT_HEADER, false);
    curl_setopt($con, CURLOPT_RETURNTRANSFER, true);

    curl_setopt($con, CURLOPT_SSL_VERIFYPEER, 1);
    curl_setopt($con, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($con, CURLOPT_CAINFO, 'cacert.pem');

    $res_curl = curl_exec($con);

    if($res_curl === false){ echo 'Curl error: ' . curl_error($con);}

    curl_close($con);


    $res_txt = json_decode($res_curl, true);
    if(!isset($res_txt['error'])){
        echo 'Result: '.print_r($res_txt,true).'';
    }else{
        echo 'Erro: '.print_r($res_txt,true).'';
    }


?>

It works!有用! :) :)

It looks like the second server you are using where the cURL request fails is a Windows platform.看起来您在 cURL 请求失败的情况下使用的第二台服务器是 Windows 平台。

The problem is that cURL needs a trusted list of certificate authority certificates in order to verify the identity of the website you are connecting to but either doesn't have any or is missing the CA certificate for the site you are connecting to.问题是 cURL 需要一个受信任的证书颁发机构证书列表,以验证您正在连接的网站的身份,但没有或缺少您正在连接的网站的 CA 证书。

At the very least, adding this to your request should fix your issue:至少,将此添加到您的请求应该可以解决您的问题:

curl_setopt($con, CURLOPT_CAINFO, 'C:/path/to/GeoTrustGlobalCA.pem');

The cert contents for GeoTrustGlobalCA.pem should be: GeoTrustGlobalCA.pem的证书内容应该是:

-----BEGIN CERTIFICAT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Aq45pmp06drE57xNNB6pXE0zX5IJL4hmXXeXxx12E6nV
5fEWCRE11azbJHFwLJhWC9kXtNHjUStedejV0NxPNO3CBWaAocvmMw==
-----END CERTIFICATE-----

For maximum portability, try downloading cacert.pem from http://curl.haxx.se/docs/caextract.html and set CURLOPT_CAINFO to that file and include it with your source code.为了获得最大的可移植性,请尝试从http://curl.haxx.se/docs/caextract.html下载cacert.pem并将CURLOPT_CAINFO为该文件并将其包含在您的源代码中。

Got same issue on Windows 10. curl extension is enabled.在 Windows 10 上遇到了同样的问题。启用了 curl 扩展。 solved by adding php dir to PATH variable.通过将 php dir 添加到 PATH 变量来解决。

My computer> Properties> Advanced system settings > Environment variables.我的电脑>属性>高级系统设置>环境变量。

add php working directory (fe C:\\php) to 'Path' system variable.将 php 工作目录 (fe C:\\php) 添加到 'Path' 系统变量。

don't forget to restart Apache.不要忘记重新启动Apache。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM