CURL Error 401 - Does PHP version cause this problem? - php

I have the following code:
$body = "<SOAP-ENV:Envelope>".$data."</SOAP-ENV:Envelope>";
$ch = curl_init($FD_Add);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: text/xml"));
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "WS******._.1:********");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSLCERT, DIR_ROOT."ABCDEFG/Certs/WS******._.1.pem");
curl_setopt($ch, CURLOPT_SSLKEY, DIR_ROOT."ABCDEFG/Certs/WS******._.1.key");
curl_setopt($ch, CURLOPT_SSLKEYPASSWD, "ckp_***********");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
if($test_env){echo 'The result of your request is:<RESULT>'.$result.'</RESULT>br/>' ;}
curl_close($ch);
The response is an error 401 HTTP requires authentication. I have verified the user ID and Password to be correct. What I am wondering first is if the version of PHP could cause this error if it does not support CULROPT_AUTH as I am running PHP~v. 3.4 which from what I read does not support this option.
Anybody know if this could be the trouble?

You could probably fake it by issuing the basic auth header yourself:
$auth_header = "Authorization: Basic " . base64_encode("$user:$pass");
curl_setopt($ch, CURLOPT_HTTP_HEADER, $auth_header);
but that's only available since Curl 7.10.3 and later. Your stone-age PHP is most likely using an equally stone-age CURL.
Really... PHP 3.4 is the equivalent of driving around in a car bought from the Flintstones. You should upgrade to something more modern, like a Model-T... those horseless carriages are pretty impressive.

Related

PHP Curl fails on Ubuntu when using PHP 7.2 but works on Mac with PHP 7.3.9

I get the following error only when running getAnalysis() on my Ubuntu machine while it still works on several versions of Mac (including the newest version).
I tried troubleshooting the error by using the curl_error function however nothing was printed out.
Failed on: Ubuntu with PHP 7.2.24-0ubuntu0.18.04.2 and curl 7.58.0.
Worked on: Mac with PHP 7.3.9 and curl 7.64.1.
Error:
<HTML><HEAD><TITLE>Error</TITLE></HEAD><BODY>
An error occurred while processing your request.<p>
Reference #179.96cadf17.1580524623.c5f345a
</BODY></HTML>
Code:
function getAnalysis($text)
{
$APIKey = "APIKey";
$URL="https://www.APIURL.com/API/";
$data = json_encode(array('text' => $text));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$URL);
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout after 30 seconds
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "apikey:$APIKey");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result=curl_exec($ch);
print("======================================\n");
print($result);
print("======================================\n");
curl_close ($ch);
return $result;
}
I found the solution finally. I just need to change
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
to
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Expect:'));
The reason was my request was to large and needed to let curl know what to expect

PHP cURL GET request : You are not authorized to access this resource

I am facing varied issue. I am able to get response in POSTman but getting below error while using PHP code.
You are not authorized to access this resource
code as below:
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml','Accept: application/xml'));
$data = curl_exec($ch);
curl_close($ch);
Unfortunately, different cURL versions behave slightly different and so there is not one valid answer but several approaches that work for different cURL versions.
Here are two suggestions:
From Problems with username or pass with colon when setting CURLOPT_USERPWD
Try adding curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);, or instead CURLAUTH_BASIC.
Something that should always work:
If it won't help, add username and password directly into url like https://user:pass#host.com/path.
You shouldnt turn off certificate verification, instead, get a valid cert, they are for free using letsencrypt.
<?php
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . base64_encode($password)); //here is the change
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml','Accept: application/xml'));
$data = curl_exec($ch);
curl_close($ch);
This is a really longshot and i know that but i have seen quite a few API's that work like that and since the OP seems not to have the documentation of the API i will post this as an answer in case it helps him solve his issue.
If above does not work try to base64_encode($username) as well

Using Auth Digest header variable in PHP curl request

I am working with an API that provides a token for me to use to connect. It gives these instructions.
This token is then sent in all subsequent calls to the server in header variable Auth Digest.
I am not sure what this means. I've tried several approaches and read several stack overflow questions. Here's what I've tried. See code comments included for details.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,true);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
// I have tried setting both of these
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
// In combination of the above separately, I have tried each of these individually
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $token);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Auth Digest: ' . $token));
curl_setopt($ch, CURLOPT_POST, true);
$post_data = array('Auth Digest' => $token);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data));
curl_setopt($ch, CURLOPT_USERPWD, $token);
// Then I execute and close, either giving me a failed response and a response that says token is not valid
$response = curl_exec($ch);
$header_sent = curl_getinfo($ch, CURLINFO_HEADER_OUT);
if (!$response) {
echo $action . ' curl request failed';
return false;
}
curl_close($ch);
$response_json = json_decode($response);
var_dump($response_json);
Here are some related stackoverflow questions that I've tried to apply to my problem without success.
Curl request with digest auth in PHP for download Bitbucket private repository
Client part of the Digest Authentication using PHP POST to Web Service
How do I make a request using HTTP basic authentication with PHP curl?
I need to know what the raw http header that they're likely expecting or how I can use php curl to generate the header they're likely expecting.
An digest authorization header typically looks like:
Authorization: Digest _data_here_
So in your case, try:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
//... existing options here
$headers = array(
'Authorization: Digest ' . $token,
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
If you use CURLOPT_HTTPHEADER, that just specifies additional headers to send and doesn't require you to add all the headers there.
If the other headers you're sending all have explicit options, use those and just pass the one authorization header to CURLOPT_HTTPHEADER.

D&B API version 2, REST Authentication using PHP

I want get Authentication Token of D&B API version 2 REST methodology
http://developer.dnb.com/docs/2.0/common/authentication-process
Sample Request - Get New Token
POST https://maxcvservices.dnb.com/rest/Authentication
x-dnb-user: P2000000FD4A9DE85D848229E03507C8
x-dnb-pwd: volcano
I have already tested these credentials on Chrome Extension (Advanced REST Client), they worked fine.
So now How can I achieve this using PHP code, Please help
Thanks
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://maxcvservices.dnb.com/rest/Authentication');
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER,array('x-dnb-user: P2000000FD4A9DE85D848229E03507C8','x-dnb-pwd: volcano'));
$param_array = array();
$param_query_string = http_build_query($param_array);
curl_setopt($ch, CURLOPT_POSTFIELDS, $param_query_string);
$response = curl_exec($ch);
I had a discusstion with DnB technical team and they sent me .NET example code, I learned code and applied same logic using PHP and it worked finally.

CURL invoking API with SSL

I am using CURL to invoke an API with self signed public key in .pfx extension
function execute_curl($string,$URL)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml','SOAPAction:"http://xxxxxxxxxxxxxxxxxx/SendDoc"'));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $string);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE);
$ch_result = curl_exec($ch);
return $ch_result;
}
I cannot set the CURLOPT_SSL_VERIFYPEER to FALSE for security purpose, anyone can show me how I can properly add the certificate along with its password into curl? EDIT I am not even sure if that is the solution for it, so far I am only getting no response from it. Any additional solution or ways to make it work is very much appreciated.
Also, I am trying to make it work on 2 environment, my dev environment which is in windows and also my test server environment which is in fedora, should the certificate be in different format?

Categories