1

Possible Duplicate:
upload a file to server without using a form?

I'am able to run this succesfully on command line:

curl -v -H "a-token: myTokenValue" -H "content-type: application/xml" -X POST --data-binary @/tmp/myfile_2_3.xml -A "My Wonderful Agent" http://example.com/url

How do I get this in PHP?

Community
  • 1
  • 1
alle
  • 90
  • 6

3 Answers3

1

UPDATE: Using POST with file uploading :-)

$fileContents = file_get_contents("/tmp/myfile_2_3.xml");
$defaults = array(
    CURLOPT_CUSTOMREQUEST => "post",
    CURLOPT_HEADER => 1,
    CURLOPT_URL => "http://example.com/url",
    CURLOPT_FRESH_CONNECT => 1,
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_FORBID_REUSE => 1,
    CURLOPT_TIMEOUT => 4,
    CURLOPT_POSTFIELDS => $fileContents,
    CURLOPT_HTTPHEADER => array("a-token" => "myTokenValue", "Content-Type" => "application/xml"),
);

$ch = curl_init();
curl_setopt_array($ch, ($options + $defaults));
if( ! $result = curl_exec($ch))
{
    trigger_error(curl_error($ch));
}
curl_close($ch);
Mohamed Nuur
  • 5,536
  • 6
  • 39
  • 55
  • 1
    Because your example does nothing but a GET. It doesn't upload a file, which is what the OP wants. You've basically said "solve your problem by solving your problem". (and no, I didn't downvote, but I'm tempted) – Marc B May 18 '11 at 17:07
  • I fixed it. It uses cURL to post a file directly to a URL now, including all the headers and stuff. – Mohamed Nuur May 18 '11 at 17:25
  • @Mohamed Nuur It works, thanks. The trick is `file_get_contents()` as argument for `CURLOPT_POSTFIELDS`, not in an array. `CURLOPT_FRESH_CONNET`, `CURLOPT_FORBID_REUSE` and `CURLOPT_TIMEOUT` are not needed, in my case. – alle May 19 '11 at 07:46
0

Pass it to exec().

ceejayoz
  • 176,543
  • 40
  • 303
  • 368
  • It is what I would like to avoid. Is there any other alternative out there? – alle May 18 '11 at 17:13
  • What's wrong with this, and why didn't you say so in your question? – ceejayoz May 18 '11 at 17:19
  • I'm sorry, you're right, I didn't explain enough about it. I have the request system in a framework, where I can obviously set the cURL options and ovverride the the request method, which already gives me the response from my request. Since I have to work with the response from my request, I have to find the right way to _translate_ the cURL command into PHP's cURL options. – alle May 18 '11 at 20:28
0

Look here for how to use it without the extra overhead of file_get_contents, which was posted here.

Community
  • 1
  • 1
Mel
  • 6,077
  • 1
  • 15
  • 12
  • The `file_get_contents` function might be the solution, since the array for `CURL_POSTFIELDS` gets to the `-F` curl option, which is not what I want in my query. The `@` character to submit a filename in the `CURL_POSTFIELDS` array wants an index; it is not allowed to pass `@myFileName.ext` except in an array (from the PHP manual: _"As of PHP 5.2.0, files thats passed to this option with the @ prefix must be in array form to work."_). – alle May 18 '11 at 20:20