簡體   English   中英

使用PHP curl的IIS或Apache Web服務器身份驗證

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

我在Windows 10上使用PHP和curl設置了IIS和Apache Web服務器。 以下代碼可以正常工作。

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

?>

當我添加身份驗證時,它不起作用。

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

?>

當我在GoDaddy服務器上運行該代碼時,該代碼運行良好。

該代碼位於http://localhost/test.php
我有PHP NTS 7.0.13和cURL 7.51.0
IIS設置為FastCGI

未設置CA認證。

  • 轉到[pathToYourPHP] \\ extras \\ ssl
  • 創建一個名為cacert.pem的文件
  • 轉到從Mozilla提取的CA證書
  • 點擊cacert.pem
  • 復制內容並將其放入您的cacert.pem
  • 在php.ini中,將路徑設置為curl.cainfo =“ [pathToYourPHP] \\ extras \\ ssl \\ cacert.pem”

注意:也可以通過添加curl_setopt($ ch,CURLOPT_SSL_VERIFYPEER,FALSE)來繞過

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM