Created
April 6, 2020 16:20
-
-
Save guibranco/855c1d050c1a674c1b3d6980ecd3cba3 to your computer and use it in GitHub Desktop.
multi curl - FB - Desenvolvimento Web - https://www.facebook.com/groups/desenvolvimentoweb/permalink/3051116748280077/
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
<?php | |
$urlX = "https://jsonplaceholder.typicode.com/todos/"; //URL1 que retorna X | |
$urlY = "https://jsonplaceholder.typicode.com/users"; //URL2 que retorna Y | |
$multiHandler = curl_multi_init(); | |
//URL1 - X | |
$curlX = curl_init(); | |
curl_setopt($curlX, CURLOPT_URL, $urlX); | |
curl_setopt($curlX, CURLOPT_HEADER, 0); | |
curl_setopt($curlX, CURLOPT_RETURNTRANSFER, 1); | |
curl_multi_add_handle($multiHandler, $curlX); | |
//URL2 - Y | |
$curlY = curl_init(); | |
curl_setopt($curlY, CURLOPT_URL, $urlY); | |
curl_setopt($curlY, CURLOPT_HEADER, 0); | |
curl_setopt($curlY, CURLOPT_RETURNTRANSFER, 1); | |
curl_multi_add_handle($multiHandler, $curlY); | |
//Executa | |
do { | |
$status = curl_multi_exec($multiHandler, $active); | |
if($active) | |
curl_multi_selecT($multiHandler); | |
} while($active && $status == CURLM_OK); | |
//pega os dados | |
$result = array(); | |
$result["X"] = curl_multi_getcontent($curlX); | |
$result["Y"] = curl_multi_getcontent($curlY); | |
//Remove os handlers e encerra o cURL | |
curl_multi_remove_handle($multiHandler, $curlX); | |
curl_multi_remove_handle($multiHandler, $curlY); | |
curl_multi_close($multiHandler); | |
//Devolve como um JSON, com as chaves X e Y | |
echo json_encode($result); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment