简体   繁体   English

cURL到PHP,带有标题和用户名,但没有密码

[英]cURL to PHP with header and username but without password

I've been trying and researching very hard but I haven't found an answer. 我一直在努力研究,但是没有找到答案。

I got following cURL: 我得到了cURL:

curl "https://someurl" -H "client-id: Fsomemorecharacters8=" -u "8somemorecharacters0:" 

Testing in a command shell works perfectly. 在命令外壳中进行测试可以完美地进行。 But how can I get it to work with PHP? 但是如何使它与PHP一起使用? Usually its "username:password" but I got "username:" which is strange. 通常是它的“用户名:密码”,但是我却得到了“用户名:”,这很奇怪。

My PHP Code so far: 到目前为止,我的PHP代码:

<?php
  $ch = curl_init();
  $url = 'https://someurl';

  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_HEADER, array('client-id: FHP20c+somemorecharacters8='));
  curl_setopt($ch, CURLOPT_USERPWD, '85a15somemorecharacters4550:'); //Issue is here i think

  $output = curl_exec($ch);

  curl_close($ch);
?>

Do you have any idea how to make it work given -u "username:" instead of -u "username:password"? 您是否知道如何在给定-u“ username:”而不是-u“ username:password”的情况下使其工作?

Regards 问候

UPDATE 更新
See below for the answer! 答案见下文!

So I was able to solve it after all. 所以我毕竟能够解决它。

  1. Thanks to print curl_error($ch); 感谢print curl_error($ch); right after $output = curl_exec($ch); 就在$output = curl_exec($ch); i found out that there is a "SSL certificate problem: self signed certificate in certificate chain". 我发现“ SSL证书问题:证书链中的自签名证书”。
  2. So I needed to download the certificate and to include it in the request like so: curl_setopt ($ch, CURLOPT_CAINFO, dirname(__FILE__)."/cacert-2017-09-20.pem"); 因此,我需要下载证书并将其包含在请求中,如下所示: curl_setopt ($ch, CURLOPT_CAINFO, dirname(__FILE__)."/cacert-2017-09-20.pem"); Or you could use curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 或者您可以使用curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); which is, however, not recommended. 但是,不建议这样做。
  3. Thanks to echoing $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); 多亏了$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); I found out that my headers were missing. 我发现我的标头不见了。 I need to use CURLOPT_HTTPHEADER instead of CURLOPT_HEADER . 我需要使用CURLOPT_HTTPHEADER而不是CURLOPT_HEADER

So here is my full code: 所以这是我的完整代码:

<?php

  $ch = curl_init();
  $url = 'https://someurl';

  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__)."/cacert-2017-09-20.pem");
  //curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($ch, CURLOPT_HTTPHEADER, array('client-id: client-id: FHP20c+somemorecharacters8='));
  curl_setopt($ch, CURLOPT_USERPWD, '885a15somemorecharacters4550:');

  $output = curl_exec($ch);
  print curl_error($ch);
  $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);  

  curl_close($ch);

  file_put_contents('output.txt', $output);

  echo '<br><b>Output</b><br>' . $output;
  echo '<br><b>Status-Code</b><br>' . $status_code;
?>

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

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