Curl to php or json - php

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

Related

How can I access the Puppet API using 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");

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

Trouble with php cURL library and REST POST request

I'm having trouble with a REST POST request after the API to which I'm posting published the final release of their API. It was working without incident, and I've been told that with the new version the server is more strict regarding the type being 'application/json'. The following cli curl command works swimmingly:
cat json.txt|curl -v -k -u user:password -F 'exchangeInstance=#-;type=application/json' https://my.url.here
However, I need to execute this in code. Using the php curl libraries I've got a simple test script up that looks like this:
$post = array(
"exchangeInstance" => $json_string,
"type" => "application/json",
);
$url = 'myurlhere';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "user:pass");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$result = curl_exec($ch);
$info = curl_getinfo($ch);
var_dump($post);
var_dump($result);
echo $result;
var_dump($info);
As I read the documentation, the Content-type in the header should automatically be set to 'multipart/form' if I pass an array as CURLOPT_POSTFIELDS, and then I'm setting the type for the element pass to 'application/json' in the array.
However, the api has had no POST requests from me. And I'm getting an error from them that clearly indicates that they are receiving a GET request. How can this possibly be? What am I missing?
curl -F !== -d
$post = array(
"exchangeInstance" => sprintf('#%s;type=application/json', $json_string),
);

Neo4j using Traverser via REST HTTP

I'm trying to use Neo4js Traverser via the HTTP API.
If I use it via curl on the command line it works fine, but when I try to use it via curl through PHP I get an error all the time.
This is curl command:
curl -H Accept:application/json -H Content-Type:application/json -X POST -d '{"order":"depth first"}' http://localhost:7474/db/data/node/5/traverse/node
And this is my PHP Code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://localhost:7474/db/data/node/5/traverse/node");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept:application/json',
'Content-Type:application/json'
));
curl_setopt($ch, CURLOPT_POSTFIELDS, '"{"order": "depth first"}"');
$output = curl_exec($ch);
echo '<pre>';
var_dump(curl_getinfo($ch));
var_dump($output);
curl_close($ch);
This is the error I get:
HTTP ERROR 500
Problem accessing
/db/data/node/5/traverse/node. Reason:
java.lang.String cannot be cast to java.util.Map
Any Ideas?
Looks like you have quotes before the JSON string:
curl_setopt($ch, CURLOPT_POSTFIELDS, '"{"order": "depth first"}"');
Might want to try this:
curl_setopt($ch, CURLOPT_POSTFIELDS, '{"order": "depth first"}');
EDIT: Although better yet, I'd use json_encode with an associative array to ensure proper escaping if necessary:
$json_data = array("order" => "depth first");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($json_data));

Categories