简体   繁体   中英

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 "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? Usually its "username:password" but I got "username:" which is strange.

My PHP Code so far:

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

Regards


See below for the answer!

So I was able to solve it after all.

  1. Thanks to print curl_error($ch); right after $output = curl_exec($ch); i found out that there is a "SSL certificate problem: self signed certificate in certificate chain".
  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"); Or you could use curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); which is, however, not recommended.
  3. Thanks to echoing $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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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