Multiple curl json and json print - php

I want to access multiples urls via curl and print the son output. I've seen this: multiple cURL and output JSON? but I`am not able make it work anyway...
my code:
<?php
$urls = Array(
'URLtoJSON1',
'URLtoJSON1'
);
for($i = 0; $i < 3; $i++) {
$curl[$i] = curl_init($urls);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "YYY:XXX");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
$curl_response[$i] = curl_exec($curl);
curl_close($curl);
$data = json_decode($curl_response[$i]);
$name[$i] = $data->fullDisplayName;
$datum[$i] = $data->timestamp;
$result[$i] = $data->result;
}
// here I`d love to be able echo output $name[URLtoJSON], etc...
?>
thank you for any help.

Instead of doing a for loop, you can make a foreach loop that iterates over your $urls array by doing foreach ($urls as $key=>$url). $key will hold the index of the array (starting at 0) and $url will hold the URL.
Here is what the resulting code would look like:
$urls = Array(
'URLtoJSON1',
'URLtoJSON2'
);
foreach ($urls as $key=>$url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "YYY:XXX");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $ch_post_data);
$ch_response = curl_exec($ch);
curl_close($ch);
$data = json_decode($ch_response);
$name[$key] = $data->fullDisplayName;
$datum[$key] = $data->timestamp;
$result[$key] = $data->result;
}
Now if you want to access $name of the first URL, you would just do $echo $name[0];
You can also access $datum or $result in a similar way.

Related

PHP cURL convert to GuzzleHttp

Please, how to convert the following code to async GuzzleHttp?
In this way, php is waiting for the return of each query.
while ($p = pg_fetch_array($var)) {
$url = "https://url";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$headers = array("Content-Type: application/json");
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$data = '{"s1":"s1","number":"'.$p['number'].'"}';
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$resp = curl_exec($curl);
curl_close($curl);
var_dump($resp);
}
Here's a working example using native curl_multi_*() functions. Specifically:
curl_multi_init()
curl_multi_add_handle()
curl_multi_exec()
curl_multi_getcontent()
curl_multi_remove_handle()
curl_multi_close()
<?php
$urls = ['https://example.com/', 'https://google.com'];
$handles = [];
foreach ($urls as $url) {
$handle = curl_init();
curl_setopt($handle, CURLOPT_URL, $url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
$handles[] = $handle;
}
$multiHandle = curl_multi_init();
foreach ($handles as $handle) {
curl_multi_add_handle($multiHandle, $handle);
}
for(;;){
curl_multi_exec($multiHandle, $stillRunning);
if($stillRunning > 0){
// some handles are still downloading, sleep-wait for data to arrive
curl_multi_select($multiHandle);
}else{
// all downloads completed
break;
}
}
foreach ($handles as $handle) {
$result = curl_multi_getcontent($handle);
var_dump($result);
curl_multi_remove_handle($multiHandle, $handle);
}
curl_multi_close($multiHandle);
Now, how convert the code cUrl to GuzzleHttp?

Multiple cURL data been overwritten on PHP for each

I need to call cURL multiple times to get different JSON responses so I followed the code in the answer of this question:
Multiple curl json and json print.
Now I realize that the variable that stores the return data is only holding the information from the last URL in the array (it's been overwritten). Here is my code:
$urls = Array(
'https://example.com/projects/277199/roles.json',
'https://example.com/projects/292291/roles.json'
);
foreach ($urls as $key=>$url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "XXX:YYY");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$ch_response = curl_exec($ch);
curl_close($ch);
$rolesData = json_decode($ch_response,true);
}
print_r($rolesData); //It's only printing the data from the last element in the urls array
How do I correctly store the data?
just add [] or [$key] to $rolesData
foreach ($urls as $key=>$url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "XXX:YYY");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$ch_response = curl_exec($ch);
curl_close($ch);
$rolesData[] = json_decode($ch_response,true);
}
print_r($rolesData);

Using cURL in a foreach-loop (php)

I have many HTTPS-URLS where I have to find a Special Phrase, so I´m trying to use cURL in a foreach Loop, but it´s not working.
...
foreach($sites as $site) {
$URL = $site;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$response = curl_exec($ch);
curl_close($ch);
}
print substr($response, $start, $i);
...
If I use just a single HTTPS-URL I get the Phrase, but inside a foreach-loop it´s not working.
Can someone help me? (:
Please excuse my poor english..
this may help :) store result inside an array
$sites = ['https://stackoverflow.com','http://example.org'];
$result = [];
foreach ($sites as $site) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $site);
// User agent
curl_setopt($ch, CURLOPT_USERAGENT, "PHP Curl");
// Include header in result? (0 = yes, 1 = no)
curl_setopt($ch, CURLOPT_HEADER, 0);
// Should cURL return or print out the data? (true = return, false = print)
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Timeout in seconds
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
// execute the given URL, and return output
$result[] = curl_exec($ch);
curl_close($ch);
}
var_dump($result);

array_merge(): Argument #1 is not an array

need your help on this... got an error on my array_merge
here's my code:
//first
$url1="https://www.zopim.com/api/v2/chats";
$ch1 = curl_init();
curl_setopt($ch1, CURLOPT_URL, $url1);
curl_setopt($ch1, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch1, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch1, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
$output1 = curl_exec($ch1);
$info1 = curl_getinfo($ch1);
curl_close($ch1);
$chats1 = json_decode($output1,true);
//second
$url2="https://www.zopim.com/api/v2/chats?page=2";
$ch2 = curl_init();
curl_setopt($ch2, CURLOPT_URL, $url2);
curl_setopt($ch2, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch2, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch2, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
$output2 = curl_exec($ch2);
$info2 = curl_getinfo($ch2);
curl_close($ch2);
$chats2 = json_decode($output2,true);
$r = [];
if(is_array($chats1) && is_array($chats2))
{
foreach($chats1 as $key => $array)
{
$r[$key] = array_merge($chats2[$key], $array);
}
}
else
{
echo 'problem with json';
}
echo json_encode($r, JSON_UNESCAPED_SLASHES);
I need to combine a two json using array_merge()... im using curl authorization to call my API..
but when i try to run the code it has a error:
here's number 44 error:
This means your json_decode fails. It will fail if the string is no valid JSON. When it fails json_decode returns either null or false, so you have to check if the response is valid:
$chats1 = json_decode($output1, true);
$chats2 = json_decode($output2, true);
if ($chats1 && $chats2 && is_array($chats1) && is_array($chats2)) {
// your code goes here
}

How can I loop through a JSON Object in PHP?

I have a variable $arr in PHP. It has the following data inside it.
This is my PHP Code.
$data = array(
'Request' => 'StockStatus',
'merchant_id' => 'shipm8',
'hash' => '09335f393d4155d9334ed61385712999'
);
$url = 'https://ship2you.com/ship2you/';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_PORT, 443);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
curl_close($ch);
$arr = json_decode($result, true);
foreach ($arr as $value) {
echo $value['packagename'];
}
I want to loop through it. How can I achieve it? I tried using foreach but it gives me error. Thanks in advance.
You have to decode your CURL output string twice:-
$arr = json_decode(json_decode($result, true),true);
foreach ($arr as $value) {
echo "<pre/>";print_r($value['packagename']);
}
Note:- #Xatenev mentioned the correct thing:-
The json has escaped quotes. When a json with escaped quotes is passed to json_decode() it only removes all the escaped sequences. When calling json_decode() again, it decodes it correctly
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_PORT, 443);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
curl_close($ch);
$arr = json_decode($result);
foreach ($arr as $arr_item) {
echo $arr_item->user_id;
}
json_decode($json, true);
If the second parameter is true, it will return array. In case it is not what you are looking for, please show the code you have.

Categories