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);
Related
I have this code to POST data to an url that is receiving the data with below, with content-type header set as text/html
file_get_contents("php://input");
This is the code That I used to POST to url and it is sending the data, but without values (i'm sending an array data with key values).
$url = "http://url im sending data to";
$object = array(
"key1" => "123",
"key2" => "345",
"key3" => "567"
);
$data = http_build_query($object, '', '&');
$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_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTREDIR, 3);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
However, it's sending the data, but without the values, since I get a response back from the url saying that values are empty.
Moreover, I checked the curl_errno($ch) and it doesn't return anything so there is no error within my code (I think?)
Can someone help me out?!
Thanks in advance!
OK I solved the problem and it was pretty simple.
Just swap the http_build_query with json_encode, and make sure to only set the curl_setopt as it is.
add the bottom code also.
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
I don't know why but for some reason, if I add other curl_setopt variables like
curl_setopt($ch, CURLOPT_POST, true);
it does not work.
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?
I'm trying to send a .txt file via php curl post request to get its contents but no success.
The point is that if I make a var_dump($_FILES) as $result being $result the curl_exec($ch) response it shows me an empty array but if I try it with $_POST:
array(
[uploaded_file] =>
[name] => 'absolute/path/to/file.txt'
[mime] => 'text/plain'
[postname] => 'file.txt'
)
How can I pass that file as a $_FILES variable?
This is my curl send.php curl script:
$filedata = curl_file_create('../path/to/file.txt', 'text/plain', 'file.txt');
$array = array(
'file' => $filedata
)
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://domain_name.com/url/to/script.php');
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $array);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
$result = curl_exec($ch);
curl_close($ch);
This is the receive.php script:
if(file_exists($_FILES['uploaded_file']['tmp_name']) && is_uploaded_file($_FILES['uploaded_file']['tmp_name'])){
$contents = file_get_contents($_FILES['uploaded_file']['tmp_name']);
$data = explode('/',$contents);
}
but it's never entering the if statement...
You can't pass the file via cURL to make it appear in the $_FILES variable.
That is because the $_FILES variable is used for uploaded files using an HTML form submission.
For security reasons, the file must be encrypted first, and decrypted in the receiver page.
I suggest you to use an encryption method that let you to choose the encryption's key, such as sha1.
You can find more informations here: https://stackoverflow.com/a/15200804/2342558
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),
);
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));