Jump to content

WireHttp and Curl


psy
 Share

Recommended Posts

This may be a question for @ryan but will ask here as it relates to module development.

I've begun development on a module to integrate PushAlert (https://pushalert.co/) with ProcessWire. Had a few hiccoughs along the way due mainly to the fact that my website already had a service worker. PushAlert support has been fabulous and I can now send push notifications from my PW website via their console and still use my own service worker code for offline cache.

There are two PushAlert APIs - JS API and a REST API. Both are needed for my module and it's a WIP. Plan is when a visitor 'Allows' notifications from my site, the PA javascript API will capture the subscription ID and store it in a PW field on the user template. That bit is still to be written.

First step is to send a push notification via the PushAlert REST API to all subscribers. PushAlert doesn't have a PHP library in GitHub but its doco does have examples, eg to send a broadcast notification to all subscribers:

<?php

	$title = "Notification Title";
	$message = "Notification Message";
	$icon = "https://yourwebsite.com/icon.png";
	$url = "https://yourwebsite.com/";
	
	$apiKey = "YOUR_API_KEY";

	$curlUrl = "https://api.pushalert.co/rest/v1/send";

	//POST variables
	$post_vars = array(
		"icon" => $icon,
		"title" => $title,
		"message" => $message,
		"url" => $url,
	);

	$headers = Array();
	$headers[] = "Authorization: api_key=".$apiKey;

	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $curlUrl);
	curl_setopt($ch, CURLOPT_POST, true);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_vars));
	curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

	$result = curl_exec($ch);

	$output = json_decode($result, true);
	if($output["success"]) {
		echo $output["id"]; //Sent Notification ID
	}
	else {
		//Others like bad request
	}
?>

To send to an individual subscriber or nominated subscribers uses the same code with a few extra items in the $options array.

The module config will store the API Key and the base API URL. The functions will allow the developer to enter the appropriate slugs, eg 'send'. All good so far...

I could use their recommended CURL stuff but would rather use WireHttp.

Questions are:

  1. Is it possible to convert the above CURL stuff into a WireHttp post and if so, how, especially how to code the custom headers?
  2. I saw in the WireHttp class that the 'sendCURL' function was commented out with: 
	/**
	 * Send using CURL (coming soon)
	 *
	 * @param string $url 
	 * @param string $method
	 * @param array $options
	 * @return bool|string
	 *
	protected function sendCURL($url, $method = 'POST', $options = array()) {

3. If not (1), then when is 'soon'?

All help welcome.

psy

 

Link to comment
Share on other sites

And does your connection come up when you use curl directly? Like:

curl -H "Authorization: api_key=<insert api key here>" --data "title=Your%20Title&message=Your%20Message&icon=http://yourwebsite.com/icon.png&url=https://yourwebsite.com" https://api.pushalert.co/rest/v1/send

 

  • Like 1
Link to comment
Share on other sites

Thanks for the replies and all food for thought.

Just to get things going, I've used the PushAlert's CURL code for now in the module and it's working fine. Will experiment more with WireHTTP later.

Cheers

Link to comment
Share on other sites

Quick update... my site already had a service worker registration file, PWABuilder. Each site can only have one service worker registration. With PushAlert's help, I managed to overcome this issue. Their software dictates that they register the service worker with a predefined file name, sw.js. Politely suggested they rethink this approach as other sites will have their own service worker registration files and who knows what problems may ensue if all vendors insist on their app registering the service worker? Awaiting outcome of that recommendation.

Next, using the PushAlert cURL code, got notifications working on FF & Chrome. Haven't ventured into Safari territory yet as it involves a whole lot of baloney with Apple certificates.

Sending notifications works well providing you don't mind getting double notifications when PWABuilder is installed with page cache/offline configured. Sigh...

Plan is to continue development of PW integration with PushAlerts and, until service worker fluff is resolved, offer clients either push notification OR offline/fast page load.

Will also do best to make calls to PushAlerts using WireHttp instead of their cURL code.

Stay tuned!

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...