1

UPDATE 2:

Made 2 small changes to the code below ("echo" instead of "return" and added "exit(); to the bottom of the script") and now the code works as expected. This was using a jQuery.get. I will now test using cURL but I expect things will work the same.

Thank you to those who responded. It got me headed in the right direction.

UPDATE:

I have been away from this for a few days but I'm back at it.

I have created a little component and set up a small test. The code for the component is:

   <?php
    use Joomla\CMS\Log\Log;
    defined('_JEXEC') or die;
    JLog::addLogger(
        array('text_file' => 'reverse_log.php'), JLog::INFO, array('reverse'));
    JLog::add('You have reached com_reverse', JLog::INFO, 'reverse');
    $app = JFactory::getApplication();
    $in = $app->input;
    $text_str = "";
    $text_str =  $in->getString('text_str', 'gnirts oN');
    JLog::add('Text string is: ' . $text_str, JLog::INFO, 'reverse');
    echo "Remote: " . strrev($text_str);
    exit();

As you can see, I am retrieving a string, reversing it and returning it. In addition, I am logging a couple of messages.

This partially works. I get to my component and I log the messages. However, instead of the reversed string, I get the web page returned (the HTML of the home page of the Joomla site). Is there some way of suppressing this and getting the reversed string?

Thank you.


What seems a simple thing is proving quite puzzling to me and I need a few hints. The scenario is site A will from time to time send some data to site J (a Joomla environment) to process. Site J has no inclination that site A will do this. Site J does not initiate the exchange, site A does. This is strictly server to server. There is no human interface.

I have read the documentation and I get the impression this can be done with com_ajax with a cURL call from site A. I just can't understand how, what the relationship is between helper.php and modulename.php and what does Javascript have to do with this (there is no UI)? What I really need is an explanation of the flow. Can someone enlighten me, please.

  • We'd prefer to not provide general, vague, theoretical answers. If you can provide a simple, yet realistic demonstration of what you are trying to do and show your best coding attempt, then volunteers will have an easier time getting you back on track. Context helps us to help you (because this helps JSE to help others in the future. – mickmackusa Nov 19 at 21:19
  • Will site J respond to Site A with the processed results or does site J simple need to process the data that Site A provides and do some form of CRUD operation on the server? Either scenario just needs a simple plugin to listen for the request from Site A and handle the submitted data. There are no modules or JavaScript involved in the process. I can submit a sample for you tomorrow. – Terry Carter Nov 20 at 4:57
  • Thank you for the explanation mickmackusa. I will indeed get more specific as I go. I am one of those who is reluctant to start coding before there is a modicum of understanding... – Sheldon Seale Nov 20 at 22:22
  • Site J may simply pass back an acknowledgement (just to close the loop) but that is all. Of course, there are aspects like what if there is a failure, do I request a resend, things like that. However, 1 step at a time. – Sheldon Seale Nov 20 at 22:24
  • I'll add some custom code for you in a bit when I get home that should get you started. – Terry Carter Nov 22 at 18:27
1

As can be seen from the updates to the original question, I have been able to make my code work although I ended up not using com_ajax and built my own component based on it. There were a couple of snags along the way but by creating my own little component, I got the code behaving as I need.

To do this, I had to add a CORS header to the .htaccess file of the Joomla environment processing the data (site J from my original question) for Access Control Allow Origin (in Apache: Header set Access-Control-Allow-Origin 'https://your.originating.site') in order to gain access to the hosted environment. I kept getting a 301 without it.

Lastly, PHP wants to return an HTML file. It's what it does. The exit() command ends the script before the home page of the destination site is "returned" for you. Perhaps a little bit of a harsh way of approaching this but it works and will suit my needs.

Again, thank you to those who responded. It helped point me in the direction I needed.

To "push" data to my little component (com_reverse), I used a jQuery.get and a cURL GET.

jQuery (from Javascript, simplified for brevity)

jQuery.get("https://my.joomla.site/index.php?option=com_reverse",
{"text_str": "Text string to reverse"},
function(result)
{
    alert(result)
});

cURL (from PHP, again, simplified for brevity)

$ch = curl_init();                                  // Initialize curl
$curl_url = "https://my.joomls.site/index.php?option=com_reverse" . "&text_str=" . urlencode("String to reverse")
//curl_setopt( $ch, CURLOPT_GET, 1 );  // GET is the default
curl_setopt($ch, CURLOPT_URL, $curl_url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);  // required for redirects
$curl_result = curl_exec($ch);
curl_close($ch);
echo "cURL result: " . $curl_result;  // echos back to calling script

NOTE: cURL GET and redirecting have issues. You lose URL encoding of strings along the way and your strings will end at the first space. This isn't a huge problem for me as most of the data I will use is numeric. However, to overcome this, I simply change " " to "~" in my character strings and reverse that before the "echo". If anyone knows of a way to maintain URL encoding through 2 or 3 redirects, I would appreciate knowing how (there are a lot of cURL options I haven't investigated yet).

NOTE 2: You should incorporate some security features if you wish to do this. I will be adding a couple of checks in my final code. This was just to figure out what would work.

  • I suggest replacing spaces with %20 instead of +. stackoverflow.com/questions/5931853/… demo: 3v4l.org/gMs8P rawurlencode() seems most direct. – mickmackusa Dec 1 at 18:13
  • Thank you for the suggestion. I tried rawurlencode() but, after the 2nd redirect (there were a total of 4 with this particular test), the "system" converted the %20 to " ". So I still end up with the problem discussed in the referenced question. Tilde "~", while not ideal, isn't effected. I wonder if there is a CURLOPT_ that covers this? – Sheldon Seale Dec 2 at 19:22
  • I don't see any redirecting code in your post. Please include more of the relevant parts of your solution; knowledgeable Joom-folk may be able to refactor your work to integrate com_ajax or some otherway that isn't acting buggy with your data. – mickmackusa Dec 2 at 20:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.