如何在PHP和Azure中调试curl


How to debug curl in PHP and Azure

好的,我有一个Drupal网站,我在Azure上部署。我的应用程序执行一个API调用Marketo来触发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;
    }

请注意:

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

dvm($response, $name = NULL);在Drupal中等同于print_r

同样的代码可以在我的本地机器(OS X, Apache, PHP5.6)和Debian (Apache, PHP5.6)上运行。

我正在使用SQLite,这不是完全相关的,但我想我应该添加以防万一。

当我在本地或Debian服务器上执行此代码时,我将打印$result变量,它告诉我它是否成功。在Azure上,我只得到第一个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

不要关闭CURLOPT_SSL_VERIFYPEER,除非你的cURL连接到非证书保护的服务器。

在PHP环境中,有两种方法可以为cURL指定证书信息:

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

    curl_setopt($ch, CURLOPT_CAINFO, getcwd())。"' cert ' ca-bundle.crt");

    注:getcwd()。"' cert ' ca-bundle。返回ca-bundle.crt的绝对路径。

  2. 设置旋度。php.ini

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

    旋度。cainfo = " D: ' '网站' 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/en/function.curl-setopt.php)