-1

I tried using curl to post to a local file and it fails. Can it be done? my two management systems are on the same server and it seems unnecessary to have it traverse the entire internet system just to go to a file on the same hard drive.

Using localhost didn't do the trick.

I also tried to $_SERVER[DOCUMENT_ROOT].'/dir/to/file.php' using post data. It's for an API that is encrypted, so I'm not sure exactly how it works. It's for a billing system I have and I just realized that it sends data back (API).

It's simply post data and an XML response. I could write an html form tag and input fields and get the same result, but there isn't really anything else to know.

The main question is: Does curl have the ability to post to a local file or not?

kenorb
  • 155,785
  • 88
  • 678
  • 743
Emery King
  • 3,550
  • 23
  • 34

4 Answers4

2

it is post data. it's for an API that is encrypted so i'm not sure exactly how it works

Without further details nobody can answer then what you should do.

But if it's indeed a POST receival script on the local server, then you can send a POST request to it using the URL:

 $url = "https://$_SERVER[SERVER_NAME]/path/to/api.php";

And then receive its output from the cURL call.

 $data = curl($url)->post(1)->postdata(array("billing"=>1234345))
                   ->returntransfer(1)->exec();
 // (you would use the cumbersome curl_setopt() calls instead)

So you get a XML or JSON or whatever response.

mario
  • 144,265
  • 20
  • 237
  • 291
  • ok that works but how do i know it's not just doing the same thing as me typing in the dot com? – Emery King Dec 21 '11 at 14:51
  • it's simply post data and an xml response. i could write an html form tag and input fields and get the same result.. there isn't really anything else to know. the question is "does curl have the ability to post to a local file or not?" – Emery King Dec 21 '11 at 14:54
  • 1
    Static files cannot receive POST requests. Rephrase your question, don't repeat it. – mario Dec 21 '11 at 14:55
  • 1
    Having seen the above I think you need to ensure that in /etc/hosts you have an alias for your server name that points to 127.0.0.1 - that way you don't go round the internet. This will still work if you are using virtual hosting. If you are not using virtual hosting then you might be able to replace $_SERVER[SERVER_NAME] with 127.0.0.1 directly. However - you will probably get an SSL certificate error if you must go through https. – nwaltham Dec 21 '11 at 15:16
1

If they're on the same drive, then use file operations instead:

file_put_contents('/path/to/the/file', $contents);

Using CURL should only be done if you absolutely NEED the http layer to get involved for some reason, or if you're dealing with a remote server. Using HTTP would also mean you need to have the 'target' script be able to handle a file upload plus whatever other data you need to send, and then that script would end up having to do file operations ANYWAYS, so in effect you've gone on a round-the-world flight just so you can move from your living room to the kitchen.

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • you know what, it's for a billing system i have and i just realized that it sends data back (api).. do you think it sends it back with curl or is it just the scripts output that curl brings back? – Emery King Dec 21 '11 at 14:44
  • you can't really "send back" with curl. That'd be a completely different connection and you'd have to have some system to listen for that incoming new connection. curl will send data, then return whatever the remote script outputted. – Marc B Dec 21 '11 at 14:46
  • ok, thats what i thought. so how could i POST data to the script and return the contents another way? localhost didn't do the trick. – Emery King Dec 21 '11 at 14:48
  • curl's the easiest to use, just make sure you specifiy a full absolute url, `http://example.com/path/to/script`, and make sure that the server's firewall allows internal http requests like that. On the other hand, if it's all local, and you have control over this other script, why not simply use it as a local script, include it, and call a function to trigger the process? – Marc B Dec 21 '11 at 14:52
  • yeah, i would but it's encrypted purchased software. i don't beleive i have a firewall so the server should be accepting internal http requests? it will recognize that it's local before sending out to dns? – Emery King Dec 21 '11 at 14:59
0

file://locafilespec.ext worked for me. I had 2 files in the same folder on a linux box, in a folder that is not served by my webserver, and I used the file:// wrapper to post to file://test.php and it worked great. it's not pretty, but it'll work for dev until I move it to it's final resting place.

bill
  • 1
0

Does curl have the ability to post to a local file or not?

To curl local file, you need to setup HTTP server as file:// won't work, so:

npm install http-server -g

Then run the HTTP server in the folder where is the file:

$ http-server

See: Using node.js as a simple web server.

Then test the curl request from the command-line to localhost like:

curl http://127.0.0.1:8081/file.html

Then you can do the same in PHP.

kenorb
  • 155,785
  • 88
  • 678
  • 743