How to get the result of executing curl_multi_exec? - php

I receive data asynchronously (curl_multi_exec) from JSON.
As a result, how to divide the received data into 2 variables ($response_url and $response_url2)?
I need two variables to continue working with each JSON separately.
$urls = [
"https://rssbot.ru/1.json",
"https://rssbot.ru/2.json"
];
$mh = curl_multi_init();
$allResponse = [];
foreach($urls as $k => $url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_multi_add_handle($mh, $ch);
$allResponse[$k] = $ch;
}
$running = null;
do {
curl_multi_exec($mh, $running);
} while($running > 0);
foreach($allResponse as $id => $ch) {
$response = curl_multi_getcontent($ch);
curl_multi_remove_handle($mh, $ch);
$response = (json_decode($response));
var_dump($response);
}
curl_multi_close($mh);
echo $response_url;
echo $response_url2;
var_dump:
array(2) {
[0]=>
object(stdClass)#1 (3) {
["symbol"]=>
string(7) "XRPBUSD"
["price"]=>
string(6) "0.3400"
["time"]=>
int(1671427537235)
}
[1]=>
object(stdClass)#2 (3) {
["symbol"]=>
string(7) "MKRUSDT"
["price"]=>
string(6) "542.60"
["time"]=>
int(1671427559567)
}
}
array(3) {
[0]=>
object(stdClass)#2 (2) {
["symbol"]=>
string(6) "ETHBTC"
["price"]=>
string(10) "0.07081400"
}
[1]=>
object(stdClass)#1 (2) {
["symbol"]=>
string(6) "LTCBTC"
["price"]=>
string(10) "0.00377700"
}
[2]=>
object(stdClass)#3 (2) {
["symbol"]=>
string(6) "BNBBTC"
["price"]=>
string(10) "0.01482300"
}
}
Thanks!

$results = [];
$prev_running = $running = null;
do {
curl_multi_exec($mh, $running);
if ($running != $prev_running) {
$info = curl_multi_info_read($mh);
if (is_array($info) && ($ch = $info['handle'])) {
$content = curl_multi_getcontent($ch);
$url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
$results[$url] = ['content' => $content, 'status' => $info['result'], 'status_text' => curl_error($ch)];
}
$prev_running = $running;
}
} while ($running > 0);

Use list function.
An example :
$urls = [
"https://rssbot.ru/1.json",
"https://rssbot.ru/2.json"
];
list($a, $b) = $urls;
// $a : string(24) "https://rssbot.ru/1.json"
// $b : string(24) "https://rssbot.ru/2.json"

Related

Ordering by JSON

We are importing our JSON from an API. The JSON is pulling through fine but unordered
We want to order the JSON file by the name field, We have used uasort but it does not seem to take effect?
$url="https://dev-api.ourwebsite.com";
$ch = curl_init();
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL,$url);
// Execute
$result=curl_exec($ch);
// DUMPING THE JSON
$json=json_decode($result, true);
uasort($json, 'name');
foreach($json as $value) {
$course_name=$value["name"];
}
usort() (or uasort() if you need to keep the keys of the array) is what you need:
<?php
// mocking some data
$json = [
["name" => "paul"],
["name" => "jeff"],
["name" => "anna"]
];
uasort($json,
// this callable needs to return 1 or -1, depending on how you want it to sort
function($a, $b) {
if($a['name']>$b['name']) {
return 1;
} else {
return -1;
}
});
var_dump($json);
foreach($json as $value) {
$course_name=$value["name"];
echo $course_name."<br>";
}
// output:
array(3) {
[2]=>
array(1) {
["name"]=>
string(4) "anna"
}
[1]=>
array(1) {
["name"]=>
string(4) "jeff"
}
[0]=>
array(1) {
["name"]=>
string(4) "paul"
}
}
anna
jeff
paul

multidimensional array in curl_opts, curl_exec return false

I have a problem with json response in Symfony2.
My jobList array:
array(4) {
["id"]=>
int(1)
["jsonrpc"]=>
string(3) "2.0"
["method"]=>
string(11) "getJobsList"
["params"]=>
array(3) {
[0]=>
array(8) {
["id"]=>
int(1)
["url"]=>
string(5) "jhbjb"
["active"]=>
NULL
["ping_time"]=>
NULL
["start_time"]=>
NULL
["stop_time"]=>
NULL
["search_phrase"]=>
string(5) "jhbjh"
["phrase_type"]=>
NULL
}
[1]=>
array(8) {
["id"]=>
int(5)
["url"]=>
string(6) "kjnkjn"
["active"]=>
NULL
["ping_time"]=>
NULL
["start_time"]=>
NULL
["stop_time"]=>
NULL
["search_phrase"]=>
string(5) "kjnkn"
["phrase_type"]=>
NULL
}
[2]=>
array(8) {
["id"]=>
int(9)
["url"]=>
string(12) "http://bhbjb"
["active"]=>
NULL
["ping_time"]=>
NULL
["start_time"]=>
NULL
["stop_time"]=>
NULL
["search_phrase"]=>
string(6) "jbjhbh"
["phrase_type"]=>
NULL
}
}
}
Send function:
private function sendJobList($jobList, $agentUrl)
{
if (!empty($jobList)) {
if (!$agentUrl) {
$url = $agentUrl;
}
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: multipart/form-data'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if($jobList)
{
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($jobList));
}
$output = curl_exec($ch);
var_dump($output);die();
curl_close($ch);
return new Response(json_encode($output, true));
} else {
return new Response("Lista jest pusta");
}
}
I don't know why curl_exec returns false. I searched the answer everywhere but nothing helped. I'll be grateful for any help.
you to change the if condition if $urlagent
I think it should be wrotten in this way:
if($agenturl){
$url= $agenturl;
}
or
if(isset($agenturl)){
$url= $agenturl;
}

Remove all elements from array where integers are lower than 3

I need to delete all elements where FacetValueCount is lower than 3.
How can I do this?
This is my array: Array name is $farben
array(8) {
[0]=>
array(2) {
["FacetValueName"]=>
string(4) "Blau"
["FacetValueCount"]=>
int(5)
}
[1]=>
array(2) {
["FacetValueName"]=>
string(7) "Schwarz"
["FacetValueCount"]=>
int(3)
}
[2]=>
array(2) {
["FacetValueName"]=>
string(4) "blue"
["FacetValueCount"]=>
int(2)
}
[3]=>
array(2) {
["FacetValueName"]=>
string(4) "Grau"
["FacetValueCount"]=>
int(1)
}
}
<?php
$farben = ARRAY();
$farben[] = array('FacetValueName'=>'Blau', 'FacetValueCount' => 5);
$farben[] = array('FacetValueName'=>'Schwarz', 'FacetValueCount' => 3);
$farben[] = array('FacetValueName'=>'blue', 'FacetValueCount' => 2);
$farben[] = array('FacetValueName'=>'Grau', 'FacetValueCount' => 1);
print '<pre>'; var_dump($farben); print '</pre>';
foreach ($farben AS $key => $row) {
if ($row['FacetValueCount'] < 3) { unset($farben[$key]); }
}
print '<pre>'; var_dump($farben); print '</pre>';
?>
try this...
$farben = array_filter($farben, function($row) {
if($row["FacetValueCount"] > 3) {
return $row;
}
});

How to concat the echo output with some base URL in JSON

I am a code like below
<?php
$movie_name="Dabangg 2";
$movie_name = urlencode($movie_name);
$url="http://api.themoviedb.org/3/search/movie?api_key=accd3ddbbae37c0315fb5c8e19b815a5&query=$movie_name";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: application/json"));
$response = curl_exec($ch);
curl_close($ch);
$obj = json_decode($response);
foreach ($obj->results as $results){
echo $results->id;
echo $results->"http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w92'.poster_path.'";
}
?>
and var_dump is
object(stdClass)#1 (4) {
["page"]=>
int(1)
["results"]=>
array(1) {
[0]=>
object(stdClass)#2 (10) {
["adult"]=>
bool(false)
["backdrop_path"]=>
string(32) "/rWGMdyTydjXXh9YphuU5gQ7wJ8X.jpg"
["id"]=>
int(147405)
["original_title"]=>
string(9) "Dabangg 2"
["release_date"]=>
string(10) "2012-12-21"
["poster_path"]=>
string(32) "/3tqT7N94fY73Ft6BVepnEqgwWyr.jpg"
["popularity"]=>
float(0.46)
["title"]=>
string(9) "Dabangg 2"
["vote_average"]=>
float(5.8)
["vote_count"]=>
int(2)
}
}
["total_pages"]=>
int(1)
["total_results"]=>
int(1)
}
my code is working fine. I just want to retrieve the poster_path with the base URL http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w92 . HOw can I do this? The above
echo $results->"http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w92'.poster_path.'";
is just my attempt to echo the full URL.
The output URL echo should be
http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w92/3tqT7N94fY73Ft6BVepnEqgwWyr.jpg
Thanks
Unless im missing something...
echo 'http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w92' . $results->poster_path;
This might help you:
<?php
$url_ahead = 'http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w92';
$poster_path = $results[0]['poster_path']; //or something like this
//$poster_path = "/3tqT7N94fY73Ft6BVepnEqgwWyr.jpg"; //for static
// echo $url_ahead.''.$poster_path;
echo $url_ahead.$poster_path;
?>
$baseUrl = "http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w92";
foreach ($obj->results as $results){
echo $results[0]->id; //this also should be changed from $results->id
$posterUrl = $results[0]['poster_path'];
echo $baseUrl.$posterUrl; //Can store like $poster_path = $baseUrl.$posterUrl;
}
Why need to use $results[0]->id and $results[0]['poster_path']? Because $results is also a Two/Multidimensional array.

Need help in array manipulation (php)

I have this $record array
array(4) {
[0]=> array(2) { ["ROLE_ID"]=> string(1) "2" ["SUBFUNCTION_ID"]=> string(3) "904" }
[1]=> array(2) { ["ROLE_ID"]=> string(1) "2" ["SUBFUNCTION_ID"]=> string(3) "903" }
[2]=> array(2) { ["ROLE_ID"]=> string(1) "2" ["SUBFUNCTION_ID"]=> string(3) "902" }
[3]=> array(2) { ["ROLE_ID"]=> string(1) "2" ["SUBFUNCTION_ID"]=> string(3) "901" }
}
How can i manipulate it so it will become like this?
array("901","902","903","904");
Thanks in advance
$subfunctionIds = array();
foreach($record as $values) {
$subFunctionIds[] = $values['SUBFUNCTION_ID'];
}
// If you want them reversed like in your example output...
$subFunctionIds = array_reverse($subFunctionIds);
var_dump($subFunctionIds);
function fetch($row) {
return $row["SUBFUNCTION_ID"];
}
$result = array_map("fetch", $record);
sort($result);
var_dump($result);
in 5.3+ you could do better:
$result = array_map(function ($row) { return $row["SUBFUNCTION_ID"]; }, $record);
sort($result);
var_dump($result);
Try doing this:
foreach ($array as $row ) {
$response[] = $row["SUBFUNCTION_ID"];
}
print_r($response);

Categories