Need help with php CURL and JSON

I am trying to do a request to get a consumer key. I already have my API key and API secret.

Here is a link to the company’s documentation on their API. This is the page to request the consumer key.

Here is their script to do the request for the consumer key. It is not in PHP and I need it to be in PHP. If I have to I may need to use their JSON example but using PHP encoding I think.

I have started on the PHP script to do the request for the consumer key. I am not sure about how I code the request for the consumer key. Do I do this with JSON or with the post fields. Here is my script.

<?php 

// PHP CURL TO GET CONSUMER KEY

error_reporting(E_ALL);
ini_set('display_errors',1);
ini_set('error_reporting', E_ALL);
ini_set('display_startup_errors',1);
error_reporting(-1);

// Variables
$url = "https://eu.api.ovh.com/auth/?credentialToken=iQ1joJE0OmSPlUAoSw1IvAPWDeaD87ZM64HEDvYq77IKIxr4bIu6fU8OtrPQEeRh";
$apiKey = "sIbir3gYt75JPUnI";


$ch = curl_init($url);

curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authentication: ' . $apiKey ));
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_POSTFIELDS);
$response = curl_exec($ch);
$result = json_decode($response);

print_r($result);

?>

Is there a reason you aren’t using the PHP API wrapper?

to quote

This PHP package is a lightweight wrapper for OVH APIs. That’s the easiest way to use OVH.com APIs in your PHP applications.

I understand the lightweight wrapper I get off github. My issue is that I need to get the consumer key. I went to their page to get the key and it had some error. They have a script that looks like shell for Linux with JSON to get the consumer key. I do not have shell access with GoDaddy. I just need to be able to run the shell and json script to get the consumer key using php. I am not familiar with JSON. I don’t do PHP all the time. I just need a little assistance getting my PHP CURL script ready to get the consumer key then I can move on to the API with the github wrapper. Can you help with my curl script to get the consumer key?

You would add the JSON-encoded data to curl_setopt ($ch, CURLOPT_POSTFIELDS), eg:

curl_setopt ($ch, CURLOPT_POSTFIELDS, 
'{
    "accessRules": [
        {
            "method": "GET",
            "path": "/*"
        }
    ],
    "redirection":"https://www.mywebsite.com/"
}');

I think you may also need to include the Content-type header, eg:

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authentication: ' . $apiKey, 
'content-type: application/json'
));

See if that works.

If not, I wonder whether you need to make this request from your web server or if you could just run the cURL command from OVH’s docs on your local machine, and not bother with PHP?

POSTFIELDS is expecting a URLEncoded string, or an array.

It can also be a JSON-encoded string.

Interesting, that’s not in the manual.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.