如何在 PHP 和 Azure 中调试 curl
How to debug curl in PHP and Azure
好的,我有一个正在 Azure 上部署的 Drupal 站点。我的应用程序对 Marketo 执行 API 调用以触发 Marketo 的 API.
中的操作
在我的本地开发人员和 Debian 服务器(常规 lamp 堆栈)上一切正常。
当我将站点部署到 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
到第二个 NULL
。
变量($this->id
、$this->getToken()
等)设置正确,我可以很好地打印它们。就像我说的,所有都在本地工作。
我错过了什么?
Azure 上的默认设置 CURLOPT_SSL_VERIFYPEER 设置为 TRUE。如果您没有 SSL,则可能存在问题。
以下摘自 MSDN 上的 post (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 指定证书信息。
在 cURL 选项中指定 CURLOPT_CAINFO:(示例代码)
curl_setopt($ch, CURLOPT_CAINFO, getcwd() . "\cert\ca-bundle.crt");
注意:getcwd() 。 "\cert\ca-bundle.crt" returns 你的 ca-bundle.crt 的绝对路径。确保 ca-bundle 安装在正确的路径。
在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
CURLOPT_SSL_VERIFYHOST 选项与 verify peer 一起使用,此选项的默认值为 2,以检查是否存在通用名称并验证它是否与提供的主机名匹配(更多详细信息在 http://php.net/manual/en/function.curl-setopt.php)
好的,我有一个正在 Azure 上部署的 Drupal 站点。我的应用程序对 Marketo 执行 API 调用以触发 Marketo 的 API.
中的操作在我的本地开发人员和 Debian 服务器(常规 lamp 堆栈)上一切正常。
当我将站点部署到 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
到第二个 NULL
。
变量($this->id
、$this->getToken()
等)设置正确,我可以很好地打印它们。就像我说的,所有都在本地工作。
我错过了什么?
Azure 上的默认设置 CURLOPT_SSL_VERIFYPEER 设置为 TRUE。如果您没有 SSL,则可能存在问题。
以下摘自 MSDN 上的 post (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 指定证书信息。
在 cURL 选项中指定 CURLOPT_CAINFO:(示例代码)
curl_setopt($ch, CURLOPT_CAINFO, getcwd() . "\cert\ca-bundle.crt");
注意:getcwd() 。 "\cert\ca-bundle.crt" returns 你的 ca-bundle.crt 的绝对路径。确保 ca-bundle 安装在正确的路径。
在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
CURLOPT_SSL_VERIFYHOST 选项与 verify peer 一起使用,此选项的默认值为 2,以检查是否存在通用名称并验证它是否与提供的主机名匹配(更多详细信息在 http://php.net/manual/en/function.curl-setopt.php)