Help

I am looking for a tutor to help me develop an api and PHP MySQL connections

Topic Labels: Automations
487 2
cancel
Showing results for 
Search instead for 
Did you mean: 

Good morning. I would like a tutor to help me integrate OneSignal with Airtable.
I could pass job details by direct message

2 Replies 2

I’m not familiar with OneSignal, but Integromat lets you integrate OneSignal with Airtable in a low-code/no-code way:

Im not really interested in tutoring but i feel your pain so im sharing snipets of my scripts that should help you set up php api calls to airtable. hope this helps.

<?php
	//plese reference airtable api that you can find by going to your airtable base and clicking on HELP in the upper right then clicking on API documentation 
	define("AIRTABLE_KEY", "your API key here");
	define("AIRTABLE_BASES", ["Base Name 1"=>["baseID"=>"base id here", "tables"], "Base Name 2"=>["baseID"=>"base id here"]]);
	$errorMessage = ["type"=>[],"message"=>[]];//i added some of my own error detection and catching use it... dont... modify it how it works for you.

	$tempRow = [
				"records"=>[
					[
						"id"=>$airtableID,//you only need id if your updating an existing record
						"fields"=>[//the variables are just random variables i used in the past as a way of an example
							"Project Name"=>$webID."_".$projectName,
                            "Project Owner First Name"=>$projectOwnerFirstName,
							"Project Owner Last Name"=>$projectOwnerLastName
                        ]
					]
				]
			];
	
	$tempRequestValues = ["baseName"=>"Base Name 1", "tableName"=>"table name here", "data"=>$tempRow];//the baseName must match the name that appears in the defined AIRTABLE_BASES and exactly how the base name appears in air table. tableName is the exact name as it appears in airtable
	try{
		$results = airTableRequests("create", $tempRequestValues);
		if(isset($results["error"])){
			$errorMessage["type"][] = $results["error"]["type"];
			$errorMessage["message"][] = $results["error"]["message"];
		}elseif(isset($results["records"][0]["id"])){//the below is a mysqli prepared statment where im updating a record with the corresponding airtable id as an example you can remove it if you dont need it. check this link out if you need some explenation of php database connections https://www.php.net/manual/en/mysqli.construct.php
			$dbserver = "localhost";
			$password = "password";
			$username = "username";
			$dbname = "databaseName";
			date_default_timezone_set('UTC');
			$dbConnection=mysqli_connect($dbserver,$username,$password,$dbname); 
			date_default_timezone_set('America/New_York');
			$stmt = mysqli_prepare($dbConnection, "UPDATE tableName SET airtableID=? WHERE id=?");
			mysqli_stmt_bind_param($stmt, "si", $results["records"][0]["id"], $webID);
			if(mysqli_stmt_execute($stmt)){
				mysqli_stmt_close($stmt);
			}
		}
	} catch (Exception $e){
		$errorMessage["type"][] = "bad request";
		$errorMessage["message"][] = $e->getMessage();
	}
	print_r($errorMessage);



	function airTableRequests($requestType, $requestValues){
		$tempError = [];
		isset($requestValues["baseName"]) ?: $tempError[] = "baseName";
		isset($requestValues["tableName"]) ?: $tempError[] = "tableName";
		if(count($tempError)>0){
			throw new Exception('The requestValues did not include '.implode(" and ", $tempError).'.');
		}
		
		switch ($requestType){
			case "retrieve":
				if(!isset($requestValues["dataID"])){
					throw new Exception('The retrieve requestValues did not include dataID.');
				}
				$ch = curl_init();
				
				curl_setopt($ch, CURLOPT_URL, 'https://api.airtable.com/v0/'.AIRTABLE_BASES[$requestValues["baseName"]]["baseID"].'/'.rawurlencode($requestValues["tableName"]).'/'.$requestValues["dataID"]);//fyi i defined AIRTABLE_BASES at the top change that data up there to be reflective of your info
				curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
				curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
				curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer '.AIRTABLE_KEY));
				curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
				$response = json_decode(curl_exec($ch),true);
				curl_close($ch);
				return $response;
				
				break;
			case "create":
				if(!isset($requestValues["data"])){
					throw new Exception('The create requestValues did not include data.');
				}
				$ch = curl_init();
				
				curl_setopt($ch, CURLOPT_URL, 'https://api.airtable.com/v0/'.AIRTABLE_BASES[$requestValues["baseName"]]["baseID"].'/'.rawurlencode($requestValues["tableName"]));
				curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
				curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
				curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer '.AIRTABLE_KEY, 'Content-Type: application/json'));
				curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($requestValues["data"]));
				curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
				$response = json_decode(curl_exec($ch),true);
				curl_close($ch);
				return $response;
				
				break;
			case "update":
				if(!isset($requestValues["data"]["records"][0]["id"])){
					throw new Exception('The update requestValues did not include an id in the records data.');
				}
				$ch = curl_init();
				
				curl_setopt($ch, CURLOPT_URL, 'https://api.airtable.com/v0/'.AIRTABLE_BASES[$requestValues["baseName"]]["baseID"].'/'.rawurlencode($requestValues["tableName"]));
				curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
				curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
				curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer '.AIRTABLE_KEY, 'Content-Type: application/json'));
				curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($requestValues["data"]));
				curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
				$response = json_decode(curl_exec($ch),true);
				curl_close($ch);
				return $response;
				
				break;
			case "delete":
				if(!isset($requestValues["record"])){
					throw new Exception('The delete requestValues did not include record.');
				}
				
				$ch = curl_init();
				
				curl_setopt($ch, CURLOPT_URL, 'https://api.airtable.com/v0/'.AIRTABLE_BASES[$requestValues["baseName"]]["baseID"].'/'.rawurlencode($requestValues["tableName"]).'/'.rawurlencode($requestValues["record"]));
				curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
				curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
				curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer '.AIRTABLE_KEY));
				curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
				$response = json_decode(curl_exec($ch),true);
				curl_close($ch);
				return $response;
				
				break;
			default:
				throw new Exception('The requestType of '.$requestType.' does not match any existing.');
				break;
		}
	}
?>