Showing posts with label drupal. Show all posts
Showing posts with label drupal. Show all posts

Thursday, October 11, 2012

Drupal REST process update

I posted earlier about the problems I have had integrating Drupal 7 REST with an external site.  Since my last post on the topic,  it has been working flawlessly.

Today, though, I upgraded the Drupal site to use Services 7.x-3.2, and configured the site to require TLS for all traffic.

The REST connection died with no explanation.

After an hour of stressful debugging, I found two separate causes:

1. The upgrade of the Services module reset the REST resources settings. I had to re-enable parsing of application/x-www-form-urlencoded  and the node resource. http://stackoverflow.com/questions/8535820/drupal-login-via-rest-server

2. Requiring all traffic to route through https (TLS) killed the PHP cURL methods.  For a debugging quick fix, adding curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); solved the problem. For a better solution, see http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/

Wednesday, July 18, 2012

January Drupal frustration solved.

I posted on January 16 about problems integrating Drupal 7 with an external website. I had to set it aside and work on other things until this week. I finally located the cause of the problem today. As are most time-consuming problems, this had a simple solution. I just had to step away from it long enough to reset, then look through the code with a fresh outlook.

The culprit in this case was running the Drupal login function targeted at http://www.mydomain.com, but then trying to retrieve the Drupal node from http://mydomain.com. Since the domain didn't match, my session auth failed.

Tuesday, January 17, 2012

Work scratchpad for January 17

Today I am continuing my effort to retrieve contents of a Drupal node in an authenticated session. I started by setting up the XMLRPC server in Drupal 7 Services 3.

8:40 a.m. update:
Using the Poster plugin for Firefox, I was able to successfully retrieve a node while logged into Drupal in Firefox by POSTing this to mysite.com/drupal-folder/xmlrpc-endpoint/node/ :

<?xml version="1.0"?>
<methodCall>
 <methodName>node.retrieve</methodName>
 <struct>
  <member>
    <name>nid</name>
    <value><i4>19</i4></value>
   </member>
 </struct>
</methodCall>

And, predictably, when I logged out of Drupal in Firefox, the same POST from Poster was rejected as being from an anonymous user. Good.

I found this valuable comment on StackOverflow:
If you're using Drupal 7 you must be using Services 3 which doesn't have a node.get method (or node.save as it happens). They've been replaced with node.retrieve and node.create & node.update respectively.
Update 9:10

I was able to get a successful XMLRPC login response and user object from Drupal by POSTing this from Firefox Poster:
<?xml version="1.0" encoding="iso-8859-1"?>
<methodCall>
<methodName>user.login</methodName>
<params>
 <param>
  <value>
   <string>example.user</string>
  </value>
 </param>
 <param>
  <value>
   <string>password</string>
  </value>
 </param>
</params> 
</methodCall>

Next step, to capture session auth data and learn how to send it back for the node request.

Update 12:15

So, it turns out that our webhost doesn't support the php_xmlrpc extension on our server. They do provide the PEAR XML_RPC package. Unfortunately, the encoding results are very different and the PEAR-produced object does not seem to be compatible with what Drupal wants. I posted a question about this difference on StackOverflow. Now, back to digging...

Update 13:40

I got a helpful response to my StackOverflow question from mario. He suggested:


To get the XML marshalled output you first construct a message instead, and then use 
the ->serialize()  method:
$msg = new XML_RPC_Message("function", array(new XML_RPC_Value(123, "int")));

print $msg->serialize(); 

I tested that code, and the print result was:

<?xml version="1.0" encoding="UTF-8"?><methodCall><methodName>function</methodName><params><param><value><int>123</int></value></param></params></methodCall>

...exactly what I needed. Now I'll modify the example code into the actual parameters and see what I get.

Update 15:00

I spent lots of time installing PEAR XML_RPC and XML_RPC2 packages and trying to locate and repair their dependencies. No success on version 2, which was the recommended solution.

Using the old XML_RPC package, I can talk to the Drupal server and get my user authenticated. However, I have been unable to parse the server's response. I can see the raw XML it returns if I do a PHP var_dump, but attempts to use the XML_RPC methods fail.  The var_dump shows it is a proper XML_RPC object, but trying to use the ->value() method as used in the example at http://pear.php.net/manual/en/package.webservices.xml-rpc.examples.php results in fatal errors.

Example code from above reference:
if (!$resp->faultCode()) {
$val = $resp->value();
$data = XML_RPC_decode($val);
echo $data[0]['name'] . ' is at version ' . $data[0]['version'];
} else {
/*
* Display problems that have been gracefully cought and
* reported by the xmlrpc.php script
*/
echo 'Fault Code: ' . $resp->faultCode() . "\n";
echo 'Fault Reason: ' . $resp->faultString() . "\n";
}
I get this:
Fatal error: Call to a member function kindOf() on a non-object in /mypath/php/XML/RPC.php on line 1948
 even though the var_dump of $resp shows as:

object(XML_RPC_Response)#4
 Frustrated. I'm going to try another method.

Monday, January 16, 2012

Drupal frustrations continue

Remember those old cartoons where the characters would open a door, only to discover another door behind it, then another, and another?  I have used that visual to illustrate the frustrations of software development to non-technical people. There is just no end of bugs to find and fix.Once one is repaired, another pops up.

I have been tracking my current problem down for more than three days now. All I want to do is retrieve a node from Drupal as XML so I can manipulate it in my own PHP coding. It should be simple, and it would be if the Drupal site I was trying to retrieve from was public.

There seems to be a bug or undiscovered setting that is preventing me from retrieving node content from my Drupal installation, which disallows anonymous users entirely.

As I posted earlier, I found an excellent sample of code posted by MichaelCole, and have been using it as my primary reference. Unfortunately, it seems that when trying to do an HTTP GET, passing the session auth data back to Drupal fails. I have tried countless combinations of methods to send the authentication strings, but none work.

These include PHP CURL options such as:
curl_setopt($curl, CURLOPT_COOKIESESSION, false); 
curl_setopt($curl, CURLOPT_COOKIEJAR, "cookiefile.txt");
curl_setopt($curl, CURLOPT_HTTPGET, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type: application/json')); (along with JSON-encoding the session cookie)

Multiple methods of sending the data, including:
$request_data = array(
    $sessData[0] => $sessData[1],
    "method" => "node.get",
    "nid" => 18
    );
$request_data = http_build_query($request_data, '', '&'); // Format post data as application/x-www-form-urlencoded
$service_url.="?$request_data"; //append the data as a GET qstring

According to the REST entry on Wikipedia , GET must be used to retrieve a resource, so utilizing POST as is done in the login procedure seems to not be an option.

I will keep digging at this and post up if when I find a solution (think positive).

Sunday, January 15, 2012

Drupal frustrations

After more than 10 years of building all my software and website coding from zero with primary programming languages like VB6, ASP, PHP, ActionScript and JS, I have begun adopting open-source packages due to their advanced feature sets.

My newest exploration is Drupal.  In many ways I am very impressed with its capabilities. However, one task that seems to me to be a simplistic task has cost me more than two days of work, and now it's early on a Sunday morning and it's still consuming my thoughts: How to make the content of a node visible on a non-Drupal external site.

I started by installing the Services 3 module, configuring the REST server and its endpoint. After hours of fruitless research, I finally discovered a posting with a good explanation of accessing Drupal data externally with authenticated access via REST.

My attempts to implement that were unsuccessful. As I posted on drupal.stackexchange.com, the Drupal system would recognize my login and authenticate my session, but I still can't retrieve a node's content.

I posted another request for help on any method to see Drupal node data externally, and received the suggestion to use feeds. So, this morning I have been researching that avenue.  So far, success has been very limited. Based on http://www.group42.ca/drupal_6_rss_omnibus I was able to finally see something from my Drupal site by requesting the url my-site.com/drupal-folder/rss.xml.

However, due to the purpose of my Drupal installation, it is locked down to require authenticated access for everything. At this point I am stymied by two problems using the Atom/RSS technique: I haven't seen documentation to do an auth login to request RSS data, and so far I can only see as RSS what appears on the Drupal front page.

Back to the research...