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));
Related
I'm trying to learn how I can interface with a json api.
In the documentation they give me a curl example:
If I run this as a command it works fine, gives me my data in a json format.
I thought I was on the right track with this: PHP + curl, HTTP POST sample code?
but apparently not as I can't figure out what to do with the -H portion of this command.
curl -H "APIKey:My:ApI;key;" -H "Content-Type:.../json" "https://urlofapp.com/API/GetTransaction" -d "{ 'CustomerID':'12345','EndDate':'2018-12-31','StartDate':'2018-01-01'}" > test.json
Trying to get the result into an array that I can sum and show a total of their orders for the year.
From the link I provided above I was trying to start with this:
// set post fields
$post = [
'CustomerID' => 12345,
'StartDate' => 2018-01-01,
'EndDate' => 2018-12-31,
];
$ch = curl_init('https://urlofapp.com/API/GetTransaction');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
// execute!
$response = curl_exec($ch);
// close the connection, release resources used
curl_close($ch);
// do anything you want with your response
var_dump($response);
The -h command refers the header.
Try below code,
// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://urlofapp.com/API/GetTransaction');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{ 'CustomerID':'12345','EndDate':'2018-12-31','StartDate':'2018-01-01'}");
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = 'Apikey: My:ApI;key;';
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
I used below to convert the curl command to PHP script,
https://incarnate.github.io/curl-to-php/
Hope it would be useful.
Dealing with curl directly usually ends up being a pain. There are a number of libraries that can help making calls like that much simpler.
Here are a few:
Simple and straighforward: http://requests.ryanmccue.info/
Has everything you could ever possibly want: https://github.com/guzzle/guzzle
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
I'm trying to do this command line curl command through PHP cURL, I'm getting the output perfectly in commandline but not the same when I try with PHP.
curl -d "text=terrible" http://text-processing.com/api/sentiment/
Tried to write a PHP cURL but I didn't get any output.
$data = "text=terrible";
$ch = curl_init('http://text-processing.com/api/sentiment/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
echo "<pre>";
print_r($response);
echo "</pre>";
Their API states they require the post to be form encoded data here sentiment api.
Excerpt
To analyze the sentiment of some text, do an HTTP POST to http://text-processing.com/api/sentiment/ with form encoded data containg the text you want to analyze. You’ll get back a JSON object response with 2 attributes:
To do this in PHP with curl, you must pass the header for Content-Type with the value application/x-www-form-urlencoded.
Demonstration
$data = "text=terrible";
$ch = curl_init('http://text-processing.com/api/sentiment/');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
echo "<pre>";
print_r($response);
echo "</pre>";
I think curl is not enable on your end. I tried your code and it is running properly.
Try to enable curl then try it.
Open your php.ini, situated in apache\bin.
Uncomment ;extension=php_curl.dll.
Restart Apache.
Hope this will help
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);
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),
);