简体   繁体   English

如何在PHP和Azure中调试curl

[英]How to debug curl in PHP and Azure

Ok, I've got a Drupal site which I'm deploying on Azure. 好的,我有一个Drupal网站,该网站正在Azure上进行部署。 My App perform an API call to Marketo to trigger an action in Marketo's API. 我的应用程序执行对Marketo的API调用,以触发Marketo的API中的操作。

On my local dev and on a Debian server (regular lamp stack) all works fine. 在我的本地开发人员和Debian服务器(常规灯组)上,一切正常。

When I deploy the site to Azure, it doesn't work and all I get is NULL . 当我将网站部署到Azure时,它不起作用,我得到的只是NULL

Here's my code: 这是我的代码:

    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;
    }

Please note: 请注意:

There are 2 calls: one to get the access_token and another the actual trigger. 有2个调用:一个调用获取access_token,另一个调用实际触发器。

dvm($response, $name = NULL); in simplistic terms is a Drupal equivalent to print_r 简单来说,Drupal等同于print_r

This exact same code works on my local machine (OS X, Apache, PHP5.6) and on Debian (Apache, PHP 5.6). 此完全相同的代码可在我的本地计算机(OS X,Apache,PHP5.6)和Debian(Apache,PHP 5.6)上运行。

I'm using SQLite which is not exactly relevant but I though I should add just in case. 我使用的SQLite并不完全相关,但我还是以防万一。

When I execute this code locally or on the Debian server I get the $result variable printed which tells me whether it was successful or not. 当我在本地或在Debian服务器上执行此代码时,我得到了$result变量,该变量告诉我它是否成功。 On Azure I get just NULL for the first FALSE to the second. 在Azure上,对于第一个FALSE到第二个FALSE ,我只会得到NULL

The variables ( $this->id , $this->getToken() , etc) are correctly set and I can I print them fine. 变量( $this->id$this->getToken()等)已正确设置,我可以很好地打印它们。 Like I said, all works locally. 就像我说的,所有作品都在本地进行。

What am I missing? 我想念什么?

Default settings on Azure has CURLOPT_SSL_VERIFYPEER set equal to TRUE. Azure上的默认设置的CURLOPT_SSL_VERIFYPEER设置为TRUE。 If you dont have an SSL there could be an issue. 如果您没有SSL,可能会有问题。

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

Common error messages related to SSL_VERIFYPEER option could be: 与SSL_VERIFYPEER选项相关的常见错误消息可能是:

SSL certificate problem, verify that the CA cert is OK SSL certificate problem: unable to get local issuer certificate SSL证书问题,请验证CA证书是否正确SSL证书问题:无法获取本地颁发者证书

The error is usually caused by missing or having invalid SSL certificate in cURL option. 该错误通常是由于cURL选项中缺少SSL证书或该证书无效。 If you see these messages, consider to validate SSL certificate, and check the path to CA certificate file. 如果看到这些消息,请考虑验证SSL证书,并检查CA证书文件的路径。 CA certificate must be in PEM format, for more detail about CA extract, visit http://curl.haxx.se/docs/caextract.html CA证书必须为PEM格式,有关CA提取的更多详细信息,请访问http://curl.haxx.se/docs/caextract.html

Do not turn off CURLOPT_SSL_VERIFYPEER unless your cURL connect to non certificate protected server. 除非您的cURL连接到不受证书保护的服务器,否则请不要关闭CURLOPT_SSL_VERIFYPEER。

There are two ways that you can specify certificate info for cURL in PHP environment. 您可以通过两种方式在PHP环境中为cURL指定证书信息。

  1. Specify CURLOPT_CAINFO in cURL option: (sample code) 在cURL选项中指定CURLOPT_CAINFO :(示例代码)

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

    Note: getcwd() . 注意:getcwd()。 "\\cert\\ca-bundle.crt" returns absolute path of your ca-bundle.crt. “ \\ cert \\ ca-bundle.crt”返回ca-bundle.crt的绝对路径。 Make sure ca-bundle is installed at correct path. 确保ca-bundle安装在正确的路径。

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

    In php.ini file, find [curl] section, add this directive (sample code): 在php.ini文件中,找到[curl]部分,添加以下指令(示例代码):

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

    Note: Restart you web app after the change. 注意:更改后重新启动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 option is used along with verify peer, default value of this option is 2, to check the existence of a common name and also verify that it matches the hostname provided (more detail at http://php.net/manual/en/function.curl-setopt.php ) 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