From ac7ccbb68890b018e5ded53c4de77737984dd7e5 Mon Sep 17 00:00:00 2001 From: Arthur Schiwon Date: Fri, 31 Mar 2017 00:01:47 +0200 Subject: [PATCH 1/2] add method to set curl options MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit …with file download example --- examples/SharePoint/file_examples.php | 16 +++++++++++++++- src/Runtime/Utilities/RequestOptions.php | 12 ++++++++++++ src/Runtime/Utilities/Requests.php | 1 + 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/examples/SharePoint/file_examples.php b/examples/SharePoint/file_examples.php index 1ebcda50..928f3fd9 100644 --- a/examples/SharePoint/file_examples.php +++ b/examples/SharePoint/file_examples.php @@ -62,6 +62,7 @@ function processFiles(SPList $list,$targetPath) //approveFile($ctx,$file->ServerRelativeUrl); $fileName = $targetPath . "/" . basename($file->ServerRelativeUrl); downloadFile($ctx,$file->ServerRelativeUrl,$fileName); + //downloadFileAsStream($ctx,$file->ServerRelativeUrl,$fileName); } } @@ -150,4 +151,17 @@ function downloadFile(ClientRuntimeContext $ctx, $fileUrl, $targetFilePath){ $fileContent = Office365\PHP\Client\SharePoint\File::openBinary($ctx,$fileUrl); file_put_contents($targetFilePath, $fileContent); print "File {$fileUrl} has been downloaded successfully\r\n"; -} \ No newline at end of file +} + +function downloadFileAsStream(ClientRuntimeContext $ctx, $fileUrl, $targetFilePath) { + $fileUrl = rawurlencode($fileUrl); + + $fp = fopen($targetFilePath, 'w+'); + $url = $ctx->getServiceRootUrl() . "web/getfilebyserverrelativeurl('$fileUrl')/\$value"; + $options = new \Office365\PHP\Client\Runtime\Utilities\RequestOptions($url); + $options->addCurlOption(CURLOPT_FILE, $fp); + $ctx->executeQueryDirect($options); + fclose($fp); + + print "File {$fileUrl} has been downloaded successfully\r\n"; +} diff --git a/src/Runtime/Utilities/RequestOptions.php b/src/Runtime/Utilities/RequestOptions.php index d41d7b23..40f5da46 100644 --- a/src/Runtime/Utilities/RequestOptions.php +++ b/src/Runtime/Utilities/RequestOptions.php @@ -27,6 +27,7 @@ public function __construct($url, $headers = array(), $data = null, $methodType $this->UserCredentials = null; $this->Verbose = false; $this->SSLVersion = null; + $this->curlOptions = array(); } public function toArray() @@ -42,9 +43,14 @@ public function toArray() 'Verbose' => $this->Verbose, 'UserCredentials' => $this->UserCredentials, 'SSLVersion' => $this->SSLVersion, + 'CurlOptions' => $this->curlOptions, ]; } + public function addCurlOption($name, $value) + { + $this->curlOptions[$name] = $value; + } public function addCustomHeader($name, $value) { @@ -127,4 +133,10 @@ function ($k, $v) { */ public $SSLVersion; + + /** + * @var array + */ + public $curlOptions; + } diff --git a/src/Runtime/Utilities/Requests.php b/src/Runtime/Utilities/Requests.php index 13ed51ce..b6bc6fdd 100644 --- a/src/Runtime/Utilities/Requests.php +++ b/src/Runtime/Utilities/Requests.php @@ -95,6 +95,7 @@ private static function init(RequestOptions $options) $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $options->Url); curl_setopt_array($ch, self::$defaultOptions); //default options + curl_setopt_array($ch, $options->curlOptions); //custom options //include headers in response curl_setopt($ch, CURLOPT_HEADER, $options->IncludeHeaders); //include body in response From 5f591f977784788117042ea6a26f9a729ccb9088 Mon Sep 17 00:00:00 2001 From: Arthur Schiwon Date: Wed, 12 Apr 2017 14:40:50 +0200 Subject: [PATCH 2/2] specifically allow to provide a file stream to curl --- examples/SharePoint/file_examples.php | 2 +- src/Runtime/Utilities/RequestOptions.php | 13 ++++--------- src/Runtime/Utilities/Requests.php | 3 ++- 3 files changed, 7 insertions(+), 11 deletions(-) diff --git a/examples/SharePoint/file_examples.php b/examples/SharePoint/file_examples.php index 928f3fd9..afa99778 100644 --- a/examples/SharePoint/file_examples.php +++ b/examples/SharePoint/file_examples.php @@ -159,7 +159,7 @@ function downloadFileAsStream(ClientRuntimeContext $ctx, $fileUrl, $targetFilePa $fp = fopen($targetFilePath, 'w+'); $url = $ctx->getServiceRootUrl() . "web/getfilebyserverrelativeurl('$fileUrl')/\$value"; $options = new \Office365\PHP\Client\Runtime\Utilities\RequestOptions($url); - $options->addCurlOption(CURLOPT_FILE, $fp); + $options->StreamHandle = $fp; $ctx->executeQueryDirect($options); fclose($fp); diff --git a/src/Runtime/Utilities/RequestOptions.php b/src/Runtime/Utilities/RequestOptions.php index 40f5da46..26fbe63e 100644 --- a/src/Runtime/Utilities/RequestOptions.php +++ b/src/Runtime/Utilities/RequestOptions.php @@ -27,7 +27,7 @@ public function __construct($url, $headers = array(), $data = null, $methodType $this->UserCredentials = null; $this->Verbose = false; $this->SSLVersion = null; - $this->curlOptions = array(); + $this->StreamHandle = null; } public function toArray() @@ -43,15 +43,10 @@ public function toArray() 'Verbose' => $this->Verbose, 'UserCredentials' => $this->UserCredentials, 'SSLVersion' => $this->SSLVersion, - 'CurlOptions' => $this->curlOptions, + 'StreamHandle' => $this->StreamHandle, ]; } - public function addCurlOption($name, $value) - { - $this->curlOptions[$name] = $value; - } - public function addCustomHeader($name, $value) { if (is_null($this->Headers)) @@ -135,8 +130,8 @@ function ($k, $v) { /** - * @var array + * @var resource */ - public $curlOptions; + public $StreamHandle; } diff --git a/src/Runtime/Utilities/Requests.php b/src/Runtime/Utilities/Requests.php index b6bc6fdd..2793e0ed 100644 --- a/src/Runtime/Utilities/Requests.php +++ b/src/Runtime/Utilities/Requests.php @@ -95,7 +95,6 @@ private static function init(RequestOptions $options) $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $options->Url); curl_setopt_array($ch, self::$defaultOptions); //default options - curl_setopt_array($ch, $options->curlOptions); //custom options //include headers in response curl_setopt($ch, CURLOPT_HEADER, $options->IncludeHeaders); //include body in response @@ -110,6 +109,8 @@ private static function init(RequestOptions $options) //set Post Body if(isset($options->Data)) curl_setopt($ch, CURLOPT_POSTFIELDS, $options->Data); + if(is_resource($options->StreamHandle)) + curl_setopt($ch, CURLOPT_FILE, $options->StreamHandle); $options->addCustomHeader("content-length",strlen($options->Data)); //custom HTTP headers if($options->Headers)