Giter Site home page Giter Site logo

Comments (4)

PHPandAZURE avatar PHPandAZURE commented on June 11, 2024 1

The log mentions Driver 17, have you tried it with the latest Driver 18? There were some potentially related fixes there

Thank you for your suggestion!!
We define the ODBC Driver to 18 and it is effective.

    global $dbConn;
    $dbConn = false;
    $serverName = getenv('DB_SERVER_NAME');
    $database = getenv('DB_DATABASE');
    $umi = getenv('DB_UMI');
    $odbcDriver = getenv('ODBC_DRIVER');
    $conInfo=array('Driver'=>$odbcDriver,'Database'=>$database,'UID' => $umi,'Authentication' => 'ActiveDirectoryMsi','Encrypt'=>'yes','CharacterSet' => 'UTF-8');
    $dbConn = sqlsrv_connect($serverName, $conInfo);

    if (!$dbConn) {
        error_log('['.date('Y-m-d H:i:s', time()).'][SysErr] '.'can not connect db.'.PHP_EOL);
        exit;
    }

    return true;

image

from msphpsql.

v-maximko avatar v-maximko commented on June 11, 2024

The log mentions Driver 17, have you tried it with the latest Driver 18? There were some potentially related fixes there

from msphpsql.

PHPandAZURE avatar PHPandAZURE commented on June 11, 2024

thank you for your confrimation
I update the php PGM (using UMI to get token first and then using Token to connect DB) it works
This is my new code
I think this issue can be closed


<?php

echo "test get token";

//get environement variables
$identityEndpoint = getenv("IDENTITY_ENDPOINT");
echo 'IDENTITY_ENDPOINT>>';
echo $identityEndpoint;
$identityHeader = getenv("IDENTITY_HEADER");
echo 'IDENTITY_HEADER>>';
echo $identityHeader;
$tokenAuthURI = "$identityEndpoint?resource=https://database.windows.net&api-version=2019-08-01&client_id=[UMI]";

echo 'tokenAuthURI>>';
echo $tokenAuthURI;


//Create a stream
$opts = array(
'http'=> array(
'method'=>"GET",
'header'=>"X-IDENTITY-HEADER: $identityHeader"
)
);

$context = stream_context_create($opts);

echo 'context>>';
echo $context;

// Open the file using the HTTP headers set above
$file = file_get_contents($tokenAuthURI, false, $context);
echo 'file>>';
echo $file;

if($file)
{
$array = json_decode($file, true);
$accToken = $array['access_token'];

echo 'array>>';
echo $array;

echo 'accToken>>';
echo $accToken;
}

$azureServer = 'xxxx';
$azureDatabase = 'xxx';

$connectionInfo = array("Database"=>$azureDatabase, "AccessToken"=>$accToken);
$conn = sqlsrv_connect($azureServer, $connectionInfo);
if ($conn === false) {
echo "Could not connect with Azure AD Access Token.\n";
print_r(sqlsrv_errors());
} else {
echo "Connected successfully with Azure AD Access Token.\n";

    $tsql = "SELECT @@Version AS SQL_VERSION";
    $stmt = sqlsrv_query($conn, $tsql);
    if ($stmt === false) {
        echo "Failed to run the simple query .\n";
        print_r(sqlsrv_errors());
    } else {
    	       echo "Query successfully>>>";
        while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
            echo $row['SQL_VERSION'] . PHP_EOL;
        }

        sqlsrv_free_stmt($stmt);
    }

sqlsrv_close($conn);
}


?>

from msphpsql.

RamirezSzabo avatar RamirezSzabo commented on June 11, 2024

Updating to 18 worked for me too. Thank you !

from msphpsql.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.