Viewed   79 times

I am having problem with PHP curl request with basic authorization.

Here is the command line curl:

curl -H "Accept: application/product+xml" "https://{id}:{api_key}@api.domain.com/products?limit=1&offset=0"

I have tried by setting curl header in following ways but it's not working

Authorization: Basic id:api_key
or 
Authorization: Basic {id}:{api_key}

I get the response "authentication parameter in the request are missing or invalid" but I have used proper id and api_key which is working in command line curl (I tested)

Please help me.

 Answers

1

Try the following code :

$username='ABC';
$password='XYZ';
$URL='<URL>';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$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");
$result=curl_exec ($ch);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);   //get status code
curl_close ($ch);
Thursday, August 18, 2022
5

cURL is not able to verify the authenticity of the certificate being used, because the certificate for the signing authority cannot be found in the local database.

This might be symptomatic of a self signed certificate being used.

What you should do is add the certificate for the signing authority to /etc/ssl/certs/ca-certificates.crt.

What you can do is use curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);, which will disable the check that is failing.

You really should use the first option.

Wednesday, August 31, 2022
 
nc3b
 
4

Ok so finally found a solution. It helps to read documentations on the classes you use and different systems used. In my case i was trying to integrate my app with microsoft dynamics 365 ax, so i had to read up on that too.

I read a lot of documents some were related to different dynamics service but this one helped most

And since the soap service needed Authorization Header, because they were using Windows authentication, we needed to get the token from oAuth link.

https://login.windows.net/$tenantDomainName/oauth2/token

PS: the oauth2 link i knew about it from github PHPConsoleApplication

I used PHP CURL to get my authorization Token and then created a client using PHP's SoapClient Class.

Make sure you add the authorization token in the header like so:

$arrayOpt = array(    
'stream_context'  => stream_context_create(
                            array('http' =>'Authorization: Bearer tokenString')
 ));

$client = new SoapClient($wsdl, $arrayOpt);

$response = $client->serviceMethod($parameters);

var_dump($response);

And you will get the values of the method.

Friday, December 9, 2022
 
zenoo
 
1

Use strftime instead of date:

setlocale( LC_TIME, "fr_FR" );
$formated_date_new = strftime( "%d %b %Y", strtotime( $this_date ) );

strftime refernce:

http://php.net/manual/en/function.strftime.php

Monday, December 26, 2022
3

Using jq for this, as Charles Duffy's answer suggests, is a very good idea. However, if you can't or do not want to install jq here is what you can do with plain POSIX shell.

#!/bin/sh
set -e

current_ip="$(curl --silent --show-error --fail ipecho.net/plain)"
echo "IP: $current_ip"

# Update A record
curl -X PUT "https://api.cloudflare.com/client/v4/zones/ZONEIDHERE/dns_records/DNSRECORDHERE" 
    -H "X-Auth-Email: EMAILHERE" 
    -H "X-Auth-Key: AUTHKEYHERE" 
    -H "Content-Type: application/json" 
    --data @- <<END;
{
    "id": "ZONEIDHERE",
    "type": "A",
    "name": "example.com",
    "content": "$current_ip",
    "zone_name": "example.com"
}
END
Friday, November 18, 2022
 
maxx
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :