Register now and start sharing your code snippets.
-->
How to upload a file with PHP and lib_curl
PHP posted 11 months ago by christian
This is an example of how to upload a file from PHP to another server that may or may not be running PHP .
Note that using Pear’s HTTP _Request package is probably easier.
1 <?php 2 require_once('Var_Dump.php'); 3 Var_Dump::displayInit(array('display_mode' => 'HTML4_Text'), array('mode' => 'normal','offset' => 4)); 4 5 $url = 'www.aktagon.com/upload'; 6 $file = '/tmp/file_to_upload'; 7 $post_data = array(); 8 9 $post_data['username'] = ''; 10 $post_data['password'] = ''; 11 $post_data['file'] = "@$file"; 12 13 $ch = curl_init(); 14 15 curl_setopt($ch, CURLOPT_URL, $url); 16 curl_setopt($ch, CURLOPT_HTTPHEADER, array( 17 # 'Cookie: xxxx', 18 # 'Content-Type: xxxx' 19 )); 20 curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 21 curl_setopt($ch, CURLOPT_POST, true); 22 #curl_setopt($ch, CURLOPT_PROXY, '127.0.0.1:8888'); 23 24 curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); 25 26 $response = curl_exec($ch); 27 $info = curl_getinfo($ch); 28 $http_code = $info['http_code']; 29 $speed = round($info['speed_upload'] / 1024); 30 $size = round($info['size_upload'] / 1024); 31 32 if($http_code != 200) 33 { 34 echo "Upload failed with HTTP code $http_code. Uploaded $size kilobytes with $speed kilobytes/s.<br/>"; 35 echo "<h1>Response</h1><pre>$response</pre><h1>Debug information</h1>"; 36 37 Var_Dump::display($info); 38 } 39 else 40 { 41 echo "Uploaded successful. Uploaded $size kilobytes with $speed kilobytes / s.<br/>"; 42 echo "<h1>Response</h1><pre>$response</pre>"; 43 } 44 curl_close($ch); 45 ?> 46
You can remove the Var_Dump code (Pear module), if you haven’t installed it, or don’t need it.
PHP XML-RPC example using Pear's XML_RPC package
PHP posted about 1 year ago by christian
First you need to install XML _RPC and VAR _DUMP:
Installing XML _RPC
1 pear install XML_RPC 2 pear install VAR_DUMP
Example XML -RPC client
Note this code hasn’t been tested, so may contain errors.
Anyway, VAR _DUMP is great for debugging…
1 <?php 2 require_once("XML/RPC.php"); 3 require_once('Var_Dump.php'); 4 5 Var_Dump::displayInit(array('display_mode' => 'HTML4_Text'), array('mode' => 'normal','offset' => 4)); 6 7 $host = "xxx.com"; 8 $uri = "/xxx/xmlrpc"; 9 10 $function = "eat"; 11 $parameters = array( 12 new XML_RPC_Value("a"), 13 new XML_RPC_Value(0), 14 new XML_RPC_Value(10), 15 new XML_RPC_Value("b") 16 ); 17 18 $message = new XML_RPC_Message($function, $parameters); 19 20 $client = new XML_RPC_Client($uri, $host, 80); 21 $client->setDebug(1); 22 $client->proxy = "127.0.0.1"; 23 24 $result = $client->send($message); 25 26 if (!$result) { 27 echo 'Communication error: '.$client->errstr; 28 Var_Dump::display($client); 29 exit; 30 } 31 32 if($result->faultCode()) 33 { 34 echo "** ERROR **n"; 35 echo 'Fault Code: ' . $result->faultCode() . "n"; 36 echo 'Fault Reason: ' . $result->faultString() . "n"; 37 exit; 38 } 39 40 echo "<pre>".htmlentities($message->serialize()). "</pre>"; 41 42 Var_Dump::display($result); 43 44 $val = $result->value(); 45 $ticket = $val->scalarval(); 46 47 echo $ticket; 48 ?> 49
Data types
struct
1 $user_params = array( 2 'username' => new XML_RPC_Value(".."), 3 'email' => new XML_RPC_Value("..."), 4 'time' => new XML_RPC_Value(gmdate("Ymd\TH:i:s"), 'dateTime.iso8601') 5 ); 6 7 $rpc_param = new XML_RPC_Value($user_params, 'struct')
dateTime.iso8601
1 'time' => new XML_RPC_Value(gmdate("Ymd\TH:i:s"), 'dateTime.iso8601')