Skip to content

Commit b181e06

Browse files
committed
Added save() method to print received content to a file.
1 parent deb3983 commit b181e06

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

README.md

+24
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,30 @@ cURL starts and executes.
311311
public function handler(): bool
312312
```
313313

314+
### `save()`
315+
316+
After cURL is handled, it writes the content to the specified file.
317+
318+
```php
319+
public function save(string $filePath): false|int
320+
```
321+
322+
Returns the number of bytes written on success.
323+
324+
**Example :**
325+
326+
```php
327+
/** @var \InitPHP\Curl\Curl $curl */
328+
$curl = new \InitPHP\Curl\Curl();
329+
$curl->setUrl("http://example.com")
330+
->prepare()
331+
->handler();
332+
333+
if($curl->save(__DIR__ . '/example.html') === FALSE){
334+
echo "The file could not be written.";
335+
}
336+
```
337+
314338
## Credits
315339

316340
- [Muhammet ŞAFAK](https://www.muhammetsafak.com.tr) <<[email protected]>>

src/Curl.php

+20-4
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ class Curl
7070
/** @var null|resource|false */
7171
protected $curl = null;
7272

73+
/** @var null|bool|string */
74+
protected $exec = null;
75+
7376
public function __construct()
7477
{
7578
if(!\extension_loaded('curl')){
@@ -241,6 +244,19 @@ public function getError(): ?string
241244
return empty($this->error) ? null : $this->error;
242245
}
243246

247+
/**
248+
* @param string $filePath
249+
* @return false|int
250+
*/
251+
public function save(string $filePath)
252+
{
253+
$content = $this->getResponse('body');
254+
if (empty($content)) {
255+
return false;
256+
}
257+
return @\file_put_contents($filePath, $content);
258+
}
259+
244260
/**
245261
* @param int|string $key
246262
* @param mixed $value
@@ -271,7 +287,7 @@ public function handler(): bool
271287
if(!empty($this->options)){
272288
\curl_setopt_array($this->curl, $this->options);
273289
}
274-
$res = \curl_exec($this->curl);
290+
$this->exec = \curl_exec($this->curl);
275291
$this->getInfo = \curl_getinfo($this->curl);
276292
$this->error = \curl_error($this->curl);
277293
}catch (\Exception $e) {
@@ -285,20 +301,20 @@ public function handler(): bool
285301
]);
286302
\curl_reset($this->curl);
287303
\curl_close($this->curl);
304+
$this->curl = null;
288305
}
289-
return $res !== FALSE;
306+
return !empty($this->exec);
290307
}
291308

292-
293309
private function curlOptionsPrepare(): void
294310
{
295311
if(\defined('CURLOPT_PROTOCOLS')){
296312
$this->addCurlOption(\CURLOPT_PROTOCOLS, (\CURLPROTO_HTTP | \CURLPROTO_HTTPS))
297313
->addCurlOption(\CURLOPT_REDIR_PROTOCOLS, (\CURLPROTO_HTTP | \CURLPROTO_HTTPS));
298314
}
299315
$this->addCurlOption(\CURLOPT_HEADER, false)
300-
->addCurlOption(\CURLOPT_RETURNTRANSFER, false)
301316
->addCurlOption(\CURLOPT_FAILONERROR, false);
317+
$this->addCurlOption(\CURLOPT_RETURNTRANSFER, false);
302318

303319
$canFollow = $this->canFollow && $this->allowRedirects;
304320
$this->addCurlOption(\CURLOPT_FOLLOWLOCATION, $canFollow)

0 commit comments

Comments
 (0)