簡體   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