WebFaction
Community site: login faq

Hi guys

I know there are XML-RPC libraries that I should be using but that's no fun.
I want to learn how this actually works so that I understand what is happening.

So here is my attempt to authenticate to api.webfaction.com with my control panel un and pw using php.



$url="https://api.webfaction.com/";

$payload='
<?xml version="1.0"?>
<methodCall>
   <methodName>login</methodName>
   <params>
     <param>
        <value><string>' . $username . '</string></value>
     </param>
    <param>
        <value><string>' . $password . '</string></value>
     </param>
    <param>
        <value><string>' . $machine . '</string></value>
     </param>
    <param>
        <value><int>' .$version . '</int></value>
     </param>
   </params>
</methodCall>
';


$header = [];       
$header[] = 'POST / HTTP/1.0';
$header[] = 'User-Agent: Frontier/5.1.2 (WinNT)';
$header[] = 'Host: api.webfaction.com';
$header[] = 'Content-Type: text/xml';
$header[] = 'Content-length: ' . strlen($payload);



$ch = curl_init();

curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 1);        // show return headers in result (for debugging)
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);    // return to string instead of printing it

$result = curl_exec($ch);
$info = curl_getinfo($ch);

curl_close($ch);

echo "<pre>";
print_r($result);

print_r($info);



The result I get is:


HTTP/1.1 200 OK
Server: nginx
Date: Thu, 09 Mar 2017 05:50:05 GMT
Content-Type: text/xml
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Cookie
X-Frame-Options: SAMEORIGIN

faultCode 1

faultString Unknown error

The four variables are set, just not included here.

What simple thing am I missing?

Thanks all.

Roy

asked 09 Mar, 06:29

powersat's gravatar image

powersat
113
accept rate: 0%

edited 09 Mar, 14:23


You need to trim the newline from the beginning of $payload.

permanent link

answered 09 Mar, 22:34

seanf's gravatar image

seanf ♦♦
11.7k21333
accept rate: 37%

doh!

Thank you. That did indeed work.

For anyone who stumbles across this in a Google search, the payload can more easily be built using 'xmlrpc_encode_request':


$payload = xmlrp_encode_request("login", array($username, $password,$machine,$version));  

The machine name needs to be capitalized: Web555 not web555.

Version only needs to be included if you want version 2. Version 1 is default. (as of March 2017)

Note: the parameters need to be in the correct order as shown.

Also I had a user agent in my header. This doesn't seem to be necessary.

To save you further frustration, The php manual for stream_context_create contains simples examples for doing this using 'fopen' and 'file_get_contents'.

permanent link

answered 09 Mar, 22:57

powersat's gravatar image

powersat
113
accept rate: 0%

edited 09 Mar, 23:15

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×249
×72
×44
×28
×6

question asked: 09 Mar, 06:29

question was seen: 356 times

last updated: 09 Mar, 23:15

WEBFACTION
REACH US
SUPPORT
AFFILIATE PROGRAM
LEGAL
© COPYRIGHT 2003-2016 SWARMA LIMITED - WEBFACTION IS A SERVICE OF SWARMA LIMITED
REGISTERED IN ENGLAND AND WALES 5729350 - VAT REGISTRATION NUMBER 877397162
5TH FLOOR, THE OLD VINYL FACTORY, HAYES, UB3 1HA, UNITED KINGDOM