0

I have a custom PowerShell API that allows us to perform some Hyper-v functions via an API.

The call to create a VM looks like this:

http://wk-api-001.hypermice.net:8888/?command=Create-VPS%20-VmHost%20%22wk-devhyp-001.hypermice.net%22%20-Memsize%204GB%20-VMname%20%22test100%22%20-cpucount%204%20-Imagecode%201

I now need to call this from PHP, which is a pretty alien language to me and am really struggling. So far I have this:

$headers = array();
$headers[] = "Content-Type: application/json";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://wk-api-001.hypermice.net:8888/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "?command=create-vps%20-vmhost%20%22wkdevhyp-001.hypermice.net%22%20-vmname%20%22WHMCSTest01%22%20-Memsize%204gb%20-cpucount%204%20-Imagecode%201" );
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_NTLM);
curl_setopt($ch, CURLOPT_USERPWD, "frank:M0nk3ydust!!!");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
print $result;

When I execute this code, I am hitting the end point, but it is not passing the actual commands, which I have defined here on CURLOPT_POSTFIELDS.

My feeling is this is not the correct place to define this, or they are not defined correctly but I don't know enough about CURL to work it out.

Any help would be much appreciated.

6
  • 2
    Not sure if it's the reason but that leading ? in your POST data does not belong there. Do you really need POST data? Isn't the API using GET? Commented Oct 16, 2018 at 9:53
  • So if I remove CURLOPT_POST do I still put the commands in CURLOPT_POSTFIELDS? and yes the leading '?' is needed Commented Oct 16, 2018 at 9:57
  • But the request body of a typical POST request does not start with ?. Commented Oct 16, 2018 at 9:59
  • well, this could be a GET request rather than a POST request. As I said, I am not familiar with CURL, and used POST, as a POST will still work when I test it from postman, and it gives me somewhere to put the commands. I accept its not right. that's why I am here Commented Oct 16, 2018 at 10:05
  • Sorry if I've given the impression that the GET/POST terms are specific to PHP or Curl. I'm using them in their standard meaning within HTTP context. I'm totally unaware of how that concrete API works. Also note that POST requests can also accept URL parameters (they aren't incompatible with request body parames). Where did you get that URL from? If it comes from the API documentation then it's very likely than the API expects or at least accepts URL parameters. Commented Oct 16, 2018 at 10:11

2 Answers 2

1

The simplest way to issue an HTTP request using GET (I suspect that's what you actually need) is to use stream wrappers, e.g.:

$output = file_get_contents('http://wk-api-001.hypermice.net:8888/?command=Create-VPS%20-VmHost%20%22wk-devhyp-001.hypermice.net%22%20-Memsize%204GB%20-VMname%20%22test100%22%20-cpucount%204%20-Imagecode%201');

If you need to provide credentials for HTTP authentication, the HTTP wrapper seems to support that part of the Common Internet Scheme Syntax:

//<user>:<password>@<host>:<port>/<url-path>

So you can inject them into the URL:

http://frank:[email protected]:8888/…

The only caveat is you need to URL-encode the username and password whenever they contain characters that have a special meaning in a URL.

Sign up to request clarification or add additional context in comments.

1 Comment

SO replace every thing I have here with this? I need the NTLM authentication though
0
$headers = array();
$headers[] = "Content-Type: application/json";
$URLCommand = "http://wk-api-001.hypermice.net:8888/?command=start-job%20-ScriptBlock%20%7BCreate-VPS%20-VmHost%20%22wk-devhyp-001%22%20-Memsize%204gb%20-VMname%20%22Frankisace%22%20-cpucount%204%20-Imagecode%201%7D";       

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $URLCommand);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_NTLM);
curl_setopt($ch, CURLOPT_USERPWD, "frank:M0nk3ydust!!!");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

So I was using POST, when I really didn't need to and I can define the commands in the actual URI like this. I'm not sure if this is the best way, but this does work

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.