繁体   English   中英

如何在PHP和Azure中调试curl

[英]How to debug curl in PHP and Azure

好的,我有一个Drupal网站,该网站正在Azure上进行部署。 我的应用程序执行对Marketo的API调用,以触发Marketo的API中的操作。

在我的本地开发人员和Debian服务器(常规灯组)上,一切正常。

当我将网站部署到Azure时,它不起作用,我得到的只是NULL

这是我的代码:

    public function postData(){
        $url = $this->host . "/rest/v1/campaigns/" . $this->id . "/schedule.json?access_token=" . $this->getToken();
        $ch = curl_init($url);
        $requestBody = $this->bodyBuilder();
        print_r($requestBody);
        curl_setopt($ch,  CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('accept: application/json','Content-Type: application/json'));
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_VERBOSE, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $requestBody);
        curl_getinfo($ch);
        $response = curl_exec($ch);
        dvm($response, $name = NULL);
        return $response;
    }

    private function getToken(){
        $ch = curl_init($this->host . "/identity/oauth/token?grant_type=client_credentials&client_id=" . $this->clientId . "&client_secret=" . $this->clientSecret);
        curl_setopt($ch,  CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('accept: application/json',));
        curl_setopt($ch, CURLOPT_VERBOSE, true);
        $response = json_decode(curl_exec($ch));
        curl_close($ch);
        $token = $response->access_token;
        dvm($response, $name = NULL);
        return $token;
    }

请注意:

有2个调用:一个调用获取access_token,另一个调用实际触发器。

dvm($response, $name = NULL); 简单来说,Drupal等同于print_r

此完全相同的代码可在我的本地计算机(OS X,Apache,PHP5.6)和Debian(Apache,PHP 5.6)上运行。

我使用的SQLite并不完全相关,但我还是以防万一。

当我在本地或在Debian服务器上执行此代码时,我得到了$result变量,该变量告诉我它是否成功。 在Azure上,对于第一个FALSE到第二个FALSE ,我只会得到NULL

变量( $this->id$this->getToken()等)已正确设置,我可以很好地打印它们。 就像我说的,所有作品都在本地进行。

我想念什么?

Azure上的默认设置的CURLOPT_SSL_VERIFYPEER设置为TRUE。 如果您没有SSL,可能会有问题。

以下摘自MSDN上的帖子( http://blogs.msdn.com/b/azureossds/archive/2015/06/12/verify-peer-certificate-from-php-curl-for-azure-apps。 aspx

与SSL_VERIFYPEER选项相关的常见错误消息可能是:

SSL证书问题,请验证CA证书是否正确SSL证书问题:无法获取本地颁发者证书

该错误通常是由于cURL选项中缺少SSL证书或该证书无效。 如果看到这些消息,请考虑验证SSL证书,并检查CA证书文件的路径。 CA证书必须为PEM格式,有关CA提取的更多详细信息,请访问http://curl.haxx.se/docs/caextract.html

除非您的cURL连接到不受证书保护的服务器,否则请不要关闭CURLOPT_SSL_VERIFYPEER。

您可以通过两种方式在PHP环境中为cURL指定证书信息。

  1. 在cURL选项中指定CURLOPT_CAINFO :(示例代码)

    curl_setopt($ ch,CURLOPT_CAINFO,getcwd()。“ \\ cert \\ ca-bundle.crt”);

    注意:getcwd()。 “ \\ cert \\ ca-bundle.crt”返回ca-bundle.crt的绝对路径。 确保ca-bundle安装在正确的路径。

  2. 在php.ini中设置curl.cainfo

    在php.ini文件中,找到[curl]部分,添加以下指令(示例代码):

    curl.cainfo =“ D:\\ home \\ site \\ wwwroot \\ cert \\ ca-bundle.crt”

    注意:更改后重新启动Web应用程序。

      “curl.cainfo” is system level directive which has to be set in php.ini, not in .user.ini. To use this approach, need to have customer php.ini. Refer to the link to setup customer php.ini, https://github.com/projectkudu/kudu/wiki/Xdt-transform-samples#using-a-custom-phpini 

CURLOPT_SSL_VERIFYHOST选项与verify peer一起使用,此选项的默认值为2,以检查是否存在公用名并验证其是否与提供的主机名匹配(更多详细信息,请参见http://php.net/manual/zh/ function.curl-setopt.php

暂无
暂无

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

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