Note that Microsoft have published a PHP7 extension for this, but if you're still on PHP5.x, that doesn't help you. I've succeeded in connecting using a different stack: freetds,odbc,pdo.
I'm using OS X 10.11.6 (El Capitan) with Macports, PHP5.6.
I've started by creating an Azure SQL Database called mydb
on a server with a name of myserver.database.windows.net
. It's important to remember to open the firewall to your client IP address, which you do on the server.
First step is to install freetds with the ODBC driver, and its PHP connector (change php56 to the correct version of your PHP):
sudo port install freetds +odbc
sudo port install php56-odbc
Next, you need to include some lines in your configuration files:
/opt/local/etc/odbcinst.ini
[FreeTDS]
Description = ODBC for FreeTDS
Driver = /opt/local/lib/libtdsodbc.so
Setup = /opt/local/lib/libtdsodbc.so
FileUsage = 1
This tells the odbc library where to find its odbc driver.
/opt/local/etc/freetds/freetds.conf
[myserver]
host = myserver.database.windows.net
port = 1433
tds version = 7.0
This tells the freetdc library where to find your server.
/opt/local/etc/odbc.ini
[myds]
Description = Test for SQL Server on Azure
Driver = FreeTDS
Trace = Yes
TraceFile = /var/log/sql.log
Database = mydb
Servername = myserver
UserName = myusername
Password = mypassword
Port = 1433
Protocol = 7.0
ReadOnly = No
RowVersioning = No
ShowSystemTables = No
ShowOidColumn = No
FakeOidIndex = No
This creates a data source called myds
pointing at your database, enabling you to connect with the following PHP:
$conn = new PDO('odbc:myds', 'myusername', 'mypassword');
If any of this doesn't work for you, first check that the freetds installation is correct using:
tsql -S myserver -U myusername -P mypassword
And then check that the ODBC specifications are OK using:
isql -v myds myusername mypassword
Thanks to https://github.com/lionheart/django-pyodbc/wiki/Mac-setup-to-connect-to-a-MS-SQL-Server, which does the equivalent job for Python and which pointed me in the right direction for all this.