pear snippets

PHP XML-RPC example using Pear's XML_RPC package

Tagged php, pear, xml-rpc  Languages php

First you need to install XML_RPC and VAR_DUMP:

Installing XML_RPC

pear install XML_RPC
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...

<?php
  require_once("XML/RPC.php");
  require_once('Var_Dump.php');

  Var_Dump::displayInit(array('display_mode' => 'HTML4_Text'), array('mode' => 'normal','offset' => 4));
  
  $host = "xxx.com";
  $uri = "/xxx/xmlrpc";

  $function   = "eat";
  $parameters = array(
    new XML_RPC_Value("a"),
    new XML_RPC_Value(0),
    new XML_RPC_Value(10),
    new XML_RPC_Value("b")
  );
  
  $message = new XML_RPC_Message($function, $parameters);
    
  $client = new XML_RPC_Client($uri, $host, 80);
  $client->setDebug(1);
  $client->proxy = "127.0.0.1";

  $result = $client->send($message);

  if (!$result) {
    echo 'Communication error: '.$client->errstr;
    Var_Dump::display($client);
    exit;
  }

  if($result->faultCode())
  {
     echo "** ERROR **n";
     echo 'Fault Code: ' . $result->faultCode() . "n";
     echo 'Fault Reason: ' . $result->faultString() . "n";
     exit;
  }

  echo "<pre>".htmlentities($message->serialize()). "</pre>";

  Var_Dump::display($result);

  $val = $result->value();
  $ticket = $val->scalarval();
  
  echo $ticket;
?>

Data types

struct

$user_params =  array(
    'username'       => new XML_RPC_Value(".."),
    'email'          => new XML_RPC_Value("..."),
    'time'  => new XML_RPC_Value(gmdate("Ymd\TH:i:s"), 'dateTime.iso8601')
    );

$rpc_param = new XML_RPC_Value($user_params, 'struct')

dateTime.iso8601

'time'  => new XML_RPC_Value(gmdate("Ymd\TH:i:s"), 'dateTime.iso8601')

How to upload a file with PHP and lib_curl

Tagged upload, php, client, pear, file  Languages php

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.

<?php
  require_once('Var_Dump.php');
  Var_Dump::displayInit(array('display_mode' => 'HTML4_Text'), array('mode' => 'normal','offset' => 4));

  $url = 'www.aktagon.com/upload';
  $file = '/tmp/file_to_upload';
  $post_data = array();

  $post_data['username'] = '';
  $post_data['password'] = '';
  $post_data['file'] = "@$file";

  $ch = curl_init();

  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_HTTPHEADER, array(
#    'Cookie: xxxx',
#    'Content-Type: xxxx'
  ));
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  curl_setopt($ch, CURLOPT_POST, true);
  #curl_setopt($ch, CURLOPT_PROXY, '127.0.0.1:8888');

  curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);

  $response  = curl_exec($ch);
  $info      = curl_getinfo($ch); 
  $http_code = $info['http_code'];
  $speed     = round($info['speed_upload'] / 1024);
  $size      = round($info['size_upload'] / 1024);

  if($http_code != 200)
  {
    echo "Upload failed with HTTP code $http_code. Uploaded $size kilobytes with $speed kilobytes/s.<br/>";
    echo "<h1>Response</h1><pre>$response</pre><h1>Debug information</h1>";

    Var_Dump::display($info);
  }
  else
  {
    echo "Uploaded successful. Uploaded $size kilobytes with $speed kilobytes / s.<br/>";
    echo "<h1>Response</h1><pre>$response</pre>";
  }
  curl_close($ch);
?>

You can remove the Var_Dump code (Pear module), if you haven't installed it, or don't need it.