-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* docs: add examples * docs: add description to examples * docs: add streaming example
- Loading branch information
Showing
9 changed files
with
298 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
/* | ||
This example shows how to load a JSON API and interact with the response in a | ||
type-safe way. This utilises https://php.gt/json for the response object. | ||
*/ | ||
use Gt\Curl\Curl; | ||
|
||
require(__DIR__ . "/../vendor/autoload.php"); | ||
|
||
$curl = new Curl("https://catfact.ninja/fact"); | ||
$curl->exec(); | ||
$json = $curl->outputJson(); | ||
echo "Here's a cat fact: {$json->getString("fact")}"; | ||
echo PHP_EOL; | ||
echo "The fact's length is {$json->getInt("length")} characters."; | ||
echo PHP_EOL; | ||
|
||
/* Example output: | ||
Here's a cat fact: Phoenician cargo ships are thought to have brought the first domesticated cats to Europe in about 900 BC. | ||
The fact's length is 105 characters. | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
/* | ||
* This example shows how more than one Curl request can be executed | ||
* simultaneously by adding the Curl object to a CurlMulti object. This means | ||
* the total time waiting for N responses is equal to the slowest response, | ||
* rather than the sum of all responses. | ||
*/ | ||
use Gt\Curl\Curl; | ||
use Gt\Curl\CurlMulti; | ||
|
||
require(__DIR__ . "/../vendor/autoload.php"); | ||
|
||
$curlCat = new Curl("https://catfact.ninja/fact"); | ||
$curlCat->setOpt(CURLOPT_RETURNTRANSFER, true); | ||
$curlIp = new Curl("https://api.ipify.org/?format=json"); | ||
$curlIp->setOpt(CURLOPT_RETURNTRANSFER, true); | ||
$curlTimeout = new Curl("https://this-domain-name-does-not-exist.example.com/nothing.json"); | ||
$curlTimeout->setOpt(CURLOPT_RETURNTRANSFER, true); | ||
|
||
$multi = new CurlMulti(); | ||
$multi->add($curlCat); | ||
$multi->add($curlIp); | ||
$multi->add($curlTimeout); | ||
|
||
$stillRunning = 0; | ||
do { | ||
$multi->exec($stillRunning); | ||
usleep(100_000); | ||
echo "."; | ||
} | ||
while($stillRunning > 0); | ||
|
||
echo PHP_EOL; | ||
echo "Cat API response: " . $multi->getContent($curlCat) . PHP_EOL; | ||
echo "IP API response: " . $multi->getContent($curlIp) . PHP_EOL; | ||
echo "Timeout API response: " . $multi->getContent($curlTimeout) . PHP_EOL; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
/* | ||
* In the previous curl-multi example, all response data is buffered, but under | ||
* some circumstances it is useful to get the response data as soon as it's | ||
* received, even if the response hasn't completed yet. | ||
* | ||
* With CurlMulti, we can stream the data byte for byte as it's received. This | ||
* is especially useful for processing data on slow or long-running responses. | ||
* | ||
* Notice how the dots are emitted every 100 microseconds, and the content is | ||
* streamed during the execution (within the while loop). | ||
*/ | ||
|
||
use Gt\Curl\Curl; | ||
use Gt\Curl\CurlMulti; | ||
|
||
require(__DIR__ . "/../vendor/autoload.php"); | ||
|
||
$urlArray = [ | ||
"https://catfact.ninja/fact", | ||
"https://api.ipify.org/?format=json", | ||
"https://this-domain-name-does-not-exist.example.com/nothing.json", | ||
]; | ||
|
||
$multi = new CurlMulti(); | ||
|
||
foreach($urlArray as $url) { | ||
$curl = new Curl($url); | ||
$curl->setOpt(CURLOPT_HEADERFUNCTION, function ($ch, string $rawHeader):int { | ||
echo "HEADER: $rawHeader"; | ||
return strlen($rawHeader); | ||
}); | ||
$curl->setOpt(CURLOPT_WRITEFUNCTION, function ($ch, string $rawBody):int { | ||
echo "BODY: $rawBody\n"; | ||
return strlen($rawBody); | ||
}); | ||
$multi->add($curl); | ||
} | ||
|
||
$stillRunning = 0; | ||
do { | ||
$multi->exec($stillRunning); | ||
usleep(10_000); | ||
echo "."; | ||
} | ||
while($stillRunning > 0); | ||
|
||
echo PHP_EOL; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.