I have some code that I think is not PHP. I need to use an api secret and api key to get a consumer key from an what I think is an api. Can anyone help me with the right syntax to use for PHP CURL. Here is a code example I got from the site for the api. Let me know what the syntax is for php.

`$ curl -XPOST -H"X-Ovh-Application: 7kbG7Bk7S9Nt7ZSV" -H "Content-type: application/json" \
https://eu.api.ovh.com/1.0/auth/credential -d '{
"accessRules": [
{
"method": "GET",
"path": "/*"
}
],
"redirection":"https://www.mywebsite.com/"
}'
{"validationUrl":"https://eu.api.ovh.com/auth/?credentialToken=iQ1joJE0OmSPlUAoSw1IvAPWDeaD87ZM64HEDvYq77IKIxr4bIu6fU8OtrPQEeRh","consumerKey":"MtSwSrPpNjqfVSmJhLbPyr2i45lSwPU1","state":"pendingValidation"}

    Most of that is just JSON-encoded data, and is the body of the post. The rest is the command line calling the curl program and passing it some flags.

    After using curl_init to create the session handle, you'll be using curl_setopt to set those flags and attach the content, then curl_exec.

    The first option to set is to say that it is a POST request (that's CURLOPT_POST). Then the headers flagged in the command line with -H; that's done by passing an array with the CURLOPT_HTTPHEADER option. I recommend also setting the CURLOPT_RETURNTRANSFER option as well so that you can collect the body of the response.

    After that comes CURLOPT_POSTFIELDS where you put the content of the request. For robustness, I recommend writing it as a PHP array and using json_encode to create the value for that option.

      I have some questions about what to do? I wrote some curl code. I am not sure what to do with the http_postfields. I am pretty sure I might need an array. Let me tell you what I am actually doing. I need a consumer key to use in an api. When I requested the info they gave me an api key and secret. The code I already posted above is their example of doing the request for the consumer key. Here is what I have so far to do the request in php. Will this php request be the same as the json one above.

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

      ?>`

        I have never done json encoding. Do I need to have json code to pass to the json_encode function. Do I use the joson code above. I used json_decode(); It seems like you were saying encode.

        gtibbetts

        Yes, en‍code. You start with a PHP array, and use json_encode to turn it into JSON that you can then add to the cURL handle's POSTFIELDS. Then you can execute the cURL request and get back some sort of response.

        json_decode would take a JSON string and turn it back into a PHP value.

          Write a Reply...