开发者

POSTing a file via PHP curl [duplicate]

开发者 https://www.devze.com 2023-03-07 05:54 出处:网络
This question already has an answer here: Closed 11 years ago. Possible Duplicate: upload a file to server without using a form?
This question already has an answer here: Closed 11 years ago.

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 d开发者_运维知识库o I get this in PHP?


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);


Pass it to exec().


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

0

精彩评论

暂无评论...
验证码 换一张
取 消