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);
Related
I have an issue trying to convert a python GET request to php curl.
This is what the python command looks like :
requests.get( f'{url}/report', data={'token': reporting_token, 'format': 'pdf'} )
I'm trying to do the same thing in php curl :
$ch = curl_init();
$params = array(
'token'=>'abcdefgh',
'format'=>'pdf',
)
$url = $url.'?'.http_build_query($params); //this shows http://xxx/report?token=abcdefgh&format=pdf
curl_setopt($ch, CURLOPT_URL, $url);
$result = curl_exec($ch);
This couldn't be any simpler, however I am getting the following error :
{"message":"missing parameter : token"}
OK here's the fix for anyone interested, the idea is to produce the curl command:
curl -GET -d 'token=abcdefg'
This can be achieved in php curl forcing a GET providing post fields...
curl_setopt($ch, CURLOPT_POSTFIELDS, $get);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_URL, $url);
Currently im working on box platform, and trying to upload a file to the box server. Box uses cURL to upload files, and i'm trying to send cURl requests from php. So far i've converted most of the cURL commads to php jargon, but i could't figure out how to pass in the attributes(name, path, containing folder) of the file to be uploaded.
here is the cURL
curl https://upload.box.com/api/2.0/files/content -H "Authorization: Bearer APP_USER_TOKEN" -X POST -F attributes='{"name":"Jon_Snow.jpeg", "parent":{"id":"0"}}' -F file=#Jon_Snow.jpeg
and here is the php version for the incomplete cURL command.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://upload.box.com/api/2.0/files/content");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = "Authorization: Bearer ".json_decode($accessToken, true )['access_token'];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
How do i do the last part of the cURL command which is
-F attributes='{"name":"Jon_Snow.jpeg", "parent":{"id":"0"}}' -F file=#Jon_Snow.jpeg
Edit: the suggestion of 'possible duplicate' is not accurate, i am asking of a way to add attributes to the uploaded files in the form -F attributes='{"name":"Jon_Snow.jpeg", "parent":{"id":"0"}}' i dont see how the suggested answer is relevant
Found the answer, i needed to json_encode the attributes array and then use new \CURLFile() function to create a file handle, it did NOT work with realpath()
$attributes = array('name'=>$fileName,'parent'=>array('id'=>$folderId));
$file = new \CURLFile($filePath);
$fields = array('attributes' => json_encode($attributes), 'file' => $file);
$headers = array("Authorization: Bearer ".$accessToken);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $uploadUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
$result = curl_exec($ch);
Try this: art about sending files via post and curlib
-F option means you're sending a post Field with name and content. Content in this case is from File becasue you're using # prefix
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 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:"
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));