msgraph-sdk-php icon indicating copy to clipboard operation
msgraph-sdk-php copied to clipboard

msgraph sdk php + proxy not working

Open 0x1234567890 opened this issue 11 months ago • 6 comments

Hello, I only have the option of enabling Internet access via HTTP proxy on my environment. I always get the message Fatal error: Uncaught GuzzleHttp\Exception\ConnectException: cURL error 6: Could not resolve host: login.microsoftonline.com. The domain is resolved via the proxy. I just have the assumption that the proxy configuration is not applied.

Unfortunately, the specified proxy method does not work. Under a different environment where there is no proxy in the way, the code works.

I have already tested the following in the GuzzleConfig area:

'proxy' => [
        'http' => 'http://$proxyHost:$proxyPort', 
        'https' => 'http://$proxyHost:$proxyPort', 
    ]

Unfortunately without success.

This code is supposed to send an email via MSGraph SDK from a specific mailbox via a company application (also works on environments without proxy):

<?php

require("config.php");
require_once("assets/lib/vendor/autoload.php");

use Microsoft\Kiota\Authentication\Oauth\AuthorizationCodeContext;
use Microsoft\Graph\Core\Authentication\GraphPhpLeagueAuthenticationProvider;
use Microsoft\Graph\GraphServiceClient;
use Microsoft\Kiota\Abstractions\ApiException;
use Microsoft\Kiota\Authentication\Oauth\ClientCredentialContext;
use Microsoft\Graph\Core\GraphClientFactory;
use Microsoft\Graph\GraphRequestAdapter;
use Microsoft\Graph\Graph;

use Microsoft\Graph\Generated\Users\Item\SendMail\SendMailPostRequestBody;
use Microsoft\Graph\Generated\Models\BodyType;
use Microsoft\Graph\Generated\Models\EmailAddress;
use Microsoft\Graph\Generated\Models\ItemBody;
use Microsoft\Graph\Generated\Models\Message;
use Microsoft\Graph\Generated\Models\Recipient;
use Microsoft\Graph\Generated\Models\InternetMessageHeader;

$scopes = ['https://graph.microsoft.com/.default'];

// Multi-tenant apps can use "common",
// single-tenant apps must use the tenant ID from the Azure portal
$tenantId = AZURE_TENANTID;

// Values from app registration
$clientId = AZURE_CLIENTID;
$clientSecret = AZURE_CLIENTSECRET;

// Microsoft\Kiota\Authentication\Oauth\AuthorizationCodeContext
$tokenRequestContext = new ClientCredentialContext(
    $tenantId,
    $clientId,
    $clientSecret
);
$authProvider = new GraphPhpLeagueAuthenticationProvider($tokenRequestContext);

// Proxy-Einstellungen
$proxyHost = PROXY_HOST;
$proxyPort = PROXY_PORT;

// Create HTTP client with a Guzzle config
// to specify proxy
$guzzleConfig = [
	"proxy" => "http://$proxyHost:$proxyPort"
];
$httpClient = GraphClientFactory::createWithConfig($guzzleConfig);
//$httpClient = GraphClientFactory::setClientConfig($guzzleConfig)::create();
$requestAdapter = new GraphRequestAdapter($authProvider, $httpClient);
$graphServiceClient = GraphServiceClient::createWithRequestAdapter($requestAdapter);








$graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes);

$requestBody = new SendMailPostRequestBody();
$message = new Message();
$message->setSubject('9/9/2018: concert');
$messageBody = new ItemBody();
$messageBody->setContentType(new BodyType('html'));
$messageBody->setContent('The group represents Nevada.');
$message->setBody($messageBody);
$toRecipientsRecipient1 = new Recipient();
$toRecipientsRecipient1EmailAddress = new EmailAddress();
$toRecipientsRecipient1EmailAddress->setAddress('[email protected]');
$toRecipientsRecipient1->setEmailAddress($toRecipientsRecipient1EmailAddress);
$toRecipientsArray []= $toRecipientsRecipient1;
$message->setToRecipients($toRecipientsArray);

$internetMessageHeadersInternetMessageHeader1 = new InternetMessageHeader();
$internetMessageHeadersInternetMessageHeader1->setName('x-custom-header-group-name');
$internetMessageHeadersInternetMessageHeader1->setValue('Nevada');
$internetMessageHeadersArray []= $internetMessageHeadersInternetMessageHeader1;
$internetMessageHeadersInternetMessageHeader2 = new InternetMessageHeader();
$internetMessageHeadersInternetMessageHeader2->setName('x-custom-header-group-id');
$internetMessageHeadersInternetMessageHeader2->setValue('NV001');
$internetMessageHeadersArray []= $internetMessageHeadersInternetMessageHeader2;
$message->setInternetMessageHeaders($internetMessageHeadersArray);

$requestBody->setMessage($message);

$result = $graphServiceClient->users()->byUserId('[email protected]')->sendMail()->post($requestBody)->wait();
print_r($result)
?>

A simple PHP CURL proxy test works without any problems:


$url = 'https://login.microsoftonline.com/c06xxxxxxxxxxbc7d/oauth2/v2.0/token';
// cURL-Initialisierung
$ch = curl_init();

// cURL-Optionen setzen
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PROXY, $proxyHost);
curl_setopt($ch, CURLOPT_PROXYPORT, $proxyPort);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Anfrage durchführen
$response = curl_exec($ch);

// Überprüfen auf Fehler
if(curl_errno($ch)) {
    echo 'cURL-Fehler: ' . curl_error($ch);
} else {
    // Erfolgreiche Anfrage
    echo $response;
}

// cURL schließen
curl_close($ch);

How can i solve this Problem?

0x1234567890 avatar Mar 07 '24 13:03 0x1234567890