Do you want to do a fetch only from the news website (GET requests only) or do you need to push back information from your Joomla! 3 website eg (POST request) ?
Joomla 3 HTTP Client is the same than the 1.x branch on Packagist HTTP Client 1.x
If what you want is GET only you might want to consider using com_newsfeeds and incoming rss feed.
You could use something like com_api for Joomla! 3 to do this.
One side note about security: Joomla 3.10.4 is already out you should consider backup your site and update to this version.
EDIT:
I coded a plugin to get you up and running as an example to fetch articles from a news api like newsapi.org
here is the revelant code of the plugin
<?php
declare(strict_types=1);
/**
* System - Examplenews
*
* @package Examplenews
*
* @author Alexandre ELISÉ <[email protected]>
* @copyright Copyright(c) 2009 - 2021 Alexandre ELISÉ. All rights reserved
* @license GNU General Public License version 2 or later; see LICENSE.txt
* @link https://alexapi.cloud
*/
defined('_JEXEC') or die;
use Joomla\CMS\Application\CMSApplication;
use Joomla\CMS\Http\Response;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\Input\Input;
use Joomla\Registry\Registry;
/**
* Examplenews plugin.
*
* @package Examplenews
* @since 0.1.0
*/
class PlgSystemExamplenews extends CMSPlugin
{
/**
* Application object
*
* @var CMSApplication
* @since 0.1.0
*/
protected ?CMSApplication $app = null;
/**
* Database object
*
* @var JDatabaseDriver
* @since 0.1.0
*/
protected ?JDatabaseDriver $db = null;
/**
* @param $subject
* @param $config
*/
public function __construct(&$subject, $config = [])
{
$this->autoloadLanguage = true;
parent::__construct($subject, $config);
}
/**
* Fetch top-headlines articles from newsapi.org API using the Joomla! 3.10
* Http Client and the StreamTransport
* This is meant to be sample code as an example to guide you along the way to
* achieve your goal
* @return mixed
*/
public function onAjaxExamplenews()
{
if ($this->app->isClient('administrator'))
{
return;
}
$this->handleRequest($this->app->input);
}
public function onAfterInitialise()
{
if ($this->app->isClient('administrator'))
{
return;
}
$this->handleRequest($this->app->input);
}
/**
* Provide a way to test this method independently
*
* @param \Joomla\Input\Input|null $givenInput
*
* @return mixed
*/
private function handleRequest(?Input $givenInput = null)
{
$input = $givenInput ?? $this->app->input;
//to be safe we should add a csrf-token verification
// but since
// a GET request is idempotent (does change with same input multiple times) we should be fine
// If you feel it's insecuren, uncomment the next line
// Session::checkToken('GET') || die(Text::_('JINVALID_TOKEN'));
$verb = strtoupper($input->getMethod());
try
{
switch ($verb)
{
case 'GET':
$uri = new Joomla\CMS\Uri\Uri('https://newsapi.org/v2/top-headlines');
$uri->setQuery(
[
'country' => 'ca',
'category' => 'business', 'apiKey' =>
$this->params->get('apiKey', ''),
]
);
$response = new Response;
$response->headers = [
'Content-Type' => 'application/json',
];
$response->code = 200;
$response->body = file_get_contents((string) $uri);
// Render the response
$this->render($response);
default:
throw new BadMethodCallException('HTTP Verb Unknown', 405);
}
}
catch (Throwable $throwable)
{
header('Content-Type: application/json');
echo (new Registry($throwable))->toString();
die;
}
}
/**
* @param \Joomla\CMS\Http\Response $response
*
* @return void
*/
private function render(Response $response)
{
// Clear headers first to prevent unexpected errors
// then redefined custom ones in the render method
$this->app->clearHeaders();
foreach ($response->headers as $key => $value)
{
header(sprintf('%s: %s', $key, $value));
}
http_response_code($response->code);
echo $response->body;
die;
}
}
When using the default Http Client I found a weird behaviour not allowing me to successfully do get requests that's why I directly used file_get_contents and Joomla! Uri class to do the same thing more efficiently simpler and optimized for GET requests.
You can use the fetched data in an ajax use case too on your joomla website after installing the sample plugin I provided.
https://example.org/index.php?option=com_ajax&plugin=examplenews&group=system&format=raw&tmpl=component
the plugin is on my GitHub for ease of use for you and other SO users. Direct download link
Source code of the plugin on GitHub