PHP cURL post file with array parameters --without keys - php

I need to send a POST request via PHP cURL. It needs to include both a file and an array parameter. The raw curl command should look like this:
curl "https://the.url.com"
-F file="#/path/to/file.xml"
-F 'list_item[]=foo'
-F 'list_item[]=bar'
Unfortunately the list_item[] parameter cannot have keys (i.e. list_item[0], list_item[1], etc...) otherwise the server throws an error.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://the.url.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, [
'file' => curl_file_create($file),
'list_item' => [
'foo',
'bar'
]
]);
$response = curl_exec($ch);
This must be producing the wrong curl command because it's also being rejected.
Can anyone help?

Related

How to use -data curl in PHP

With curl from linux bash I can download a webpages passing some variables
curl --data "var1=val1&var2=val2&var3=&var4=val4&btnSubmit=btnName" https://<url> -o "<fileToSave>"
I need to do the same things but to get the full html code of that page into a php variable.
I'm trying with this:
<?php $ch = curl_init();
$post_data = array (
"var1" => "val1",
"var2" => "val2",
"var3" => "",
"var4" => "val4",
"btnSubmit" => "btnName"
);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$response = curl_exec($ch);
?>
The problem is that from bash I can retrieve the html pages and save it with this code my $response value is empty.
What's wrong?
Use http_build_query() over the $post_data, otherwise curl will assume to POST multipart/form-data posting based on the Array Data type(that's why it is not working for you).
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data));
http_build_query() converts the array into key1=value1&key2=value2 formated string with automatic urlencode().
Seems like you are connecting to a HTTPs URL. You should enable this cURL parameter.
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

Curl Upload not working for ge.tt API

Hi i am trying to upload using this tutorial http://ge.tt/developers/start .On step 4 they mention something like this
curl --upload-file myfile.txt http://blobs.ge.tt/a1b2c3/myfile.txt?sig=-TR2k2-3kjsh9nfmn4
What is the equivalent php code for the above line ? i tried below code , but its not working (returning bool false)
$url = $arr["posturl"];
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$postData = array(
'file' => '#/home/nextgen/public_html/api/myfile.txt',
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
$response = curl_exec($ch);
var_dump($response);
note : i got correct $url and myfile.txt exists and i tried replacing 'file' => '#/home/nextgen/public_html/api/myfile.txt' with '#myfile.txt' ..nothing seems to work.
'#myfile.txt' must be '#/full/path/to/myfile.txt'
curl_setopt($ch, CURLOPT_POST,1); is required
If you'd used curl's --libcurl option you would've seen the difference. Your command line does PUT, your PHP version does multipart formpost...

PHP Curl equivalent of command line curl

I have a working command line curl command
curl -v -d '{"auth": {"passwordCredentials": {"username": "myusername", "password": "mypassword"}}}' -H 'Content-type: application/json' myurl
I am trying to write equivalent PHP curl command -
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, myurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
$data = array('json' => '{auth: {passwordCredentials : {username : myusername, password : mypassword }}}');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$output = curl_exec($ch);
echo $output;
I am having different response for both the calls.
I have doubt about setting the json data correctly.
Along with your curl request, also send the HTTP header. For example:
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
And the post data should be:
$post_data = '{auth: {passwordCredentials : {username : myusername, password : mypassword }}}';
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
You can use the json_encode function to make sure that things are properly encoded.
See for example https://stackoverflow.com/a/4271654/1967396 .
In your case, it might look like this:
$authData = array(auth => array(passwordCredentials => array( username => floris, password => secret )));
and then you create the POST data with
curl_setopt($ch, CURLOPT_POSTFIELDS, array('json'=>json_encode($authData)));
If you do
print json_encode($authData);
you get
{"auth":{"passwordCredentials":{"username":"floris","password":"secret"}}}
Presumably you could do this manually, without the json_encode function.

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