简体   繁体   English

使用PHP curl的IIS或Apache Web服务器身份验证

[英]IIS or Apache Web Server Authentication with PHP curl

I have IIS and Apache Web Server set up on Windows 10 with PHP and curl. 我在Windows 10上使用PHP和curl设置了IIS和Apache Web服务器。 The following code works fine. 以下代码可以正常工作。

<?php

  $url = "http://domain.com";

  $ch = curl_init($url);
  curl_setopt($ch, CURLOPT_TIMEOUT, 30); //Timeout after 30 seconds
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

  $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); //Get status code

  $response = curl_exec($ch);

  curl_close($ch);

  echo $response;

?>

When I add authentication, it does not work. 当我添加身份验证时,它不起作用。

<?php

  $username = "username";
  $password = "password";
  $url = "https://domain.com/api/v2/orders/count";

  $ch = curl_init($url);
  curl_setopt($ch, CURLOPT_TIMEOUT, 30); //Timeout after 30 seconds
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
  curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");

  $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); //Get status code

  $response = curl_exec($ch);

  curl_close($ch);

  echo $response;

?>

This code works fine when I run it on my GoDaddy server. 当我在GoDaddy服务器上运行该代码时,该代码运行良好。

The code is located at http://localhost/test.php 该代码位于http://localhost/test.php
I have PHP NTS 7.0.13 and cURL 7.51.0 我有PHP NTS 7.0.13和cURL 7.51.0
IIS is set up as FastCGI IIS设置为FastCGI

The CA Certification was not set up. 未设置CA认证。

  • Go to [pathToYourPHP]\\extras\\ssl 转到[pathToYourPHP] \\ extras \\ ssl
  • Create a file called cacert.pem 创建一个名为cacert.pem的文件
  • Go to CA certificates extracted from Mozilla 转到从Mozilla提取的CA证书
  • Click on cacert.pem 点击cacert.pem
  • Copy the contents and put it in your cacert.pem 复制内容并将其放入您的cacert.pem
  • In php.ini, set the path to curl.cainfo = "[pathToYourPHP]\\extras\\ssl\\cacert.pem" 在php.ini中,将路径设置为curl.cainfo =“ [pathToYourPHP] \\ extras \\ ssl \\ cacert.pem”

Note: This can also be bypassed by adding curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE) 注意:也可以通过添加curl_setopt($ ch,CURLOPT_SSL_VERIFYPEER,FALSE)来绕过

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM