如何在PHP和Azure中调试curl

时间:2015-10-23 09:11:05

标签: php azure curl drupal drupal-7

好的,我有一个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);是一个等同于print_r

的Drupal

这个完全相同的代码适用于我的本地机器(OS X,Apache,PHP5.6)和Debian(Apache,PHP 5.6)。

我使用的SQLite并不完全相关,但我应该添加以防万一。

当我在本地或在Debian服务器上执行此代码时,我会打印出$result变量,告诉我它是否成功。在Azure上,第一个NULL到第二个FALSE只有$this->id

变量($this->getToken(),{{1}}等)已正确设置,我可以打印好。就像我说的,都在当地有效。

我错过了什么?

1 个答案:

答案 0 :(得分:1)

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”

    注意:更改后重新启动您的网络应用程序。

         “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
    
  3. CURLOPT_SSL_VERIFYHOST选项与验证对等项一起使用,此选项的默认值为2,以检查是否存在公用名,并验证它是否与提供的主机名匹配(更多细节见http://php.net/manual/en/function.curl-setopt.php