How can I access the Puppet API using PHP - php

I am trying to get a PHP script to access the Puppet API. I have spent 2 days searching and cannot believe that I cant find any info whatsoever (only puppet modules for installing PHP).
I am just trying to use PHP and curl but I am not able to get any kind of response, error or anything. Here is my (very basic) attempt to get the cert from the puppet master:
function get_data($url) {
$request_headers = array();
$request_headers[] = 'Accept: s';
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$response = get_data('https://<puppet master>:8140/production/certificate/ca');
All I am trying to replicate is a curl call that works from my server:
curl -k -H "Accept: s" https://<puppet master>:8140/production/certificate/ca
I have a feeling that there is probably something obvious I am missing but, I cannot figure it out.

Thank to Wrikken and Glen for getting me on track. Once I set the curl_setopt options correctly, it works as expected.
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE);
curl_setopt($ch, CURLOPT_CAINFO, "/path/to/cacert.pem");

Related

Curl to php or json

i need help i've been trying to run this
curl -H "Authorization: Basic dXNlckBjb21wYW55LmNvbTp0ZXN0" https://security.voluum.com/login
but quite stuck, also is it possible to create this in php or in json? i need data to create something. also i tried replacing dXNlckBjb21wYW55LmNvbTp0ZXN0 with my own 64bit username:pass but still not working can someone explain this i really dont know what to do
In your case data for login is: user#company.com:test. And you can use curl in ssh like follows:
curl -u user#company.com:test https://security.voluum.com/login
PHP
You can also execute this in PHP as follows:
$user='user#company.com';
$pass='test';
$url='https://security.voluum.com/login';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "$user:$pass");
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); //get status code
$result=curl_exec($ch);
curl_close($ch);
echo "Status code: $status_code \n";
print_r($result);
More informations
Base64
dXNlckBjb21wYW55LmNvbTp0ZXN0 == user#company.com:test you can decode/encode it by yourself here
Curl
More about -u you can read here. And curl manual is available here
PHP
PHP:Curl Manual

Why does this CURL work in PHP but not in the shell

I'm trying to access an API. I can do this from PHP
<?php
$url = 'http://apiurl/path';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('API-KEY: myKey'));
//curl_setopt($ch, CURLOPT_POST, 1);
$output = curl_exec($ch);
$curlInfo = curl_getinfo($ch);
curl_close($ch);
echo $output; /*success!*/
However, doing this from the command line triggers a 403 error.
curl -i -v -H "API-KEY: myKey" http://apiurl/path
/*failure!*/
What is PHP doing differently? Or have I got a syntax error in my command line code?
The only obvious difference I can see is that command-line PHP adds a User-Agent header by default. Possibly the API server you're using rejects requests from the Curl user agent? Try removing it from the command-line by adding the parameter -H "User-Agent:"

PHP POST curl terminal command in PHP script

I've found at the StackOverflow answer here exactly how I want to post data to another web url.
However, I want this command to be executed in my php web script, not from the terminal. From looking at the curl documentation, I think it should be something along the lines of:
<?php
$url = "http://myurl.com";
$myjson = "{\"column1\":\"1\",\"colum2\":\"2\",\"column3\":\"3\",\"column4\":\"4\",\"column5\":\"5\",\"column6\":\"6\"}";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $myjson);
curl_close($ch);
?>
What is the best way to do that?
The current terminal command that is working is:
curl -X POST -H "Content-Type: application/json" -d '{"column1":"1","colum2":"2","column3":"3","column4":"4","column5":"5","column6":"6"}' https://myurl.com
There are two problems:
You need to properly quote $myjson. Read the difference between single and double quoted strings in PHP.
You are not sending the curl request: curl_exec()
This should move you in the right direction:
<?php
$url = 'http://myurl.com';
$myjson = '{"column1":"1","colum2":"2","column3":"3","column4":"4","column5":"5","column6":"6"}';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $myjson);
$result = curl_exec($ch);
curl_close($ch);

Trying to create a shared link in box.net using PHP

Here is my code:
$params = array();
$params['shared_link'] = array("access"=> "Open");
$params = json_encode($params);
echo $params;
$key = "[key]";
$token = "[token]";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.box.com/2.0/folders/[folder_id]/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',
"Authorization: BoxAuth api_key=$key&auth_token=$token",'Content-Length: ' . strlen($params), 'X-HTTP-Method-Override: PUT'));
$result = curl_exec($ch);
curl_close($ch);
print_r($result);
I am not being able to create a shared link. I get this response from box.net:
{"type":"error","status":500,"code":"internal_server_error","help_url":"http:\/\/developers.box.com\/docs\/#errors","message":"Internal Server Error","request_id":"79086734650bfaf56c7894"}
Can somebody, please, help me on this?
Thanks!
Marcelo
Looking at the URL returned in their response, they give this information for 500 errors:
5xx
The request is fine, but something is wrong on Box’s end
So it sounds like you would need to contact Box about the issue.
Luckily, i could solve my problem.
The problem was that i wanted to create an "open" shared link with an enterprise token and apparently it is not possible (i am not 100% sure but, as per my attempts, i think so).
Thanks everyone for the help.
Marcelo

REST API Implementation Sample Code

I know nothing about implementing an API. I do know PHP a bit. I have a situation where I need to call a REST API method to purge cache on a CDN server. Can somebody help me with some sample code?
The following is the sample request:
PUT <<url>>
Authorization: TOK:12345-12345
Accept: application/json
Content-Type: application/json
Host: api.edgecast.com
Content-Length: 87
{
"MediaPath":"<<urlhere>>"
"MediaType":"3"
}
Can somebody help me with code to implement this rest api request?
Thanks in advance.
I had to find the hard way too. This has been tested (with slight modifications from my original code)
//## GoGrid PHP REST API Call
define('TOKEN','XXXXX-XXXXX-XXXXX-XXXXXX'); // found on the cdn admin My Settings
define('ACCOUNT_NUMBER','XXXX'); // found on the cdn admin Top Right corner
function purgeCacheFileFromCDN($urlToPurge) {
//## Build the request
$request_params = (object) array('MediaPath' => $urlToPurge, 'MediaType' => 8); // MediaType 8=small 3=large
$data = json_encode($request_params);
//## setup the connection and call.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.edgecast.com/v2/mcc/customers/'.ACCOUNT_NUMBER.'/edge/purge');
curl_setopt($ch, CURLOPT_PORT , 443);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLINFO_HEADER_OUT, 1); // For debugging
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1); // no caching
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1); // no caching
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: tok:'.TOKEN, 'Content-Type: application/json','Accept: application/json', 'Content-length: '.strlen($data)));
$head = curl_exec($ch);
$httpCode = curl_getinfo($ch);
curl_close($ch);
//## check if error
if ($httpCode['http_code'] != 200) {
echo 'Error reported: '.print_r(array($head,$httpCode),1); // output it to stdout this will be emailed to me via cron capture.
}
}
Was too lazy to write from the scratch so copied from amazingly pink site that Google advises in the first page of results.
$data = array("a" => $a);
$ch = curl_init($this->_serviceUrl . $id);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));
$response = curl_exec($ch);
if(!$response) {
return false;
}
PS: The source search request: http://www.google.ru/search?q=php+sample+put+request+curl
Here is my source gist for my fully implemented Grunt task for anyone else thinking about working with the EdgeCast API. You'll find in my example that I use a node module to execute the curl command which purges the CDN.
This was that I ended up with after spending hours trying to get an HTTP request to work within Node. I was able to get one working in Ruby and Python, but did not meet the requirements of this project.

Categories