array_merge(): Argument #1 is not an array - php

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
}

Related

Curl request method inside foreach loop got a error

I'm getting an error "Undefined offset: 1" when i use foreach (option 1) instead of duplicate code (option 2). With foreach, i'm using an array only with 2 strings to test. The error is on line $html_tt = get($direct_link_tt[1]);.
Should i do something different in get method?
$cookie_path = dirname(__FILE__).'/cookie.txt';
function login($user, $pass, $account) {
$URL = 'https://example.com/index.php';
global $cookie_path;
$ch = curl_init($URL);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'url_login='.$user.'&url_password='.$pass.'&url_account='.$account);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_path);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_path);
$ret = curl_exec($ch);
curl_close($ch);
return $ret;
}
function get($url) {
global $cookie_path;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_path);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_path);
$html = curl_exec($ch);
curl_close($ch);
return $html;
}
function main() {
$user = 'XXXXXX';
$pass = 'XXXXXX';
$account = 'XXXXX';
$all_tt = getVals();
$dataLogin = login($user, $pass, $account);
preg_match('/"PHPSESSID", "(.*)"/U', $dataLogin, $phpSessionEV);
preg_match('/uniqid=([0-9]*)&/U', $dataLogin, $uniqid);
//option 1 start
foreach ($all_tt as $tt){
$url_bef_ticket = "https://example.com/ajax_widget.php?session_id=".$phpSessionEV[1]."&uniqid=".$uniqid[1]."&lng=PO&theme={C9F11D78-448E-4EAC-A766-C904ED98458E}&widgetname=InternalFilteredQuery.InternalFilteredQueryWidget&widgetid=Grid_78A562B7_B6EA_45BF_9223_A2D42C74851F&WidgetParams=action%3DgetContent%26blockGUID%3D%257BC99ECD05-3D48-4C62-ABF0-66292053AED6%257D%26eventName%3DRequestFullfimentItem%26filterGUID%3D%257B096C2D42-0DF7-4A54-9978-D13E59BAB8ED%257D%26internalGUID%3D%257B78A562B7-B6EA-45BF-9223-A2D42C74851F%257D%26title%3DPedidos%2Bde%2BServi%25C3%25A7o%26maxLines%3D5%26queryGUID%3D%257B78A562B7-B6EA-45BF-9223-A2D42C74851F%257D%26reportGUID%3D%257BC910B0A9-D202-42D4-8E75-CC48FC3EA1D8%257D%26searchText%3D".$tt."%26searchGUID%3D%257B78A562B7-B6EA-45BF-9223-A2D42C74851F%257D%26themeGUID%3D%257BC9F11D78-448E-4EAC-A766-C904ED98458E%257D";
$html_bef_ticket = get($url_bef_ticket);
preg_match('/<a class="aImg" href="(.*)"/U', $html_bef_ticket, $direct_link_tt);
$html_tt = get($direct_link_tt[1]);
print_r($html_tt);
}
//option 1 end
//option 2 start
$url_bef_t = "https://example.com/ajax_widget.php?session_id=".$phpSessionEV[1]."&uniqid=".$uniqid[1]."&lng=PO&theme={C9F11D78-448E-4EAC-A766-C904ED98458E}&widgetname=InternalFilteredQuery.InternalFilteredQueryWidget&widgetid=Grid_78A562B7_B6EA_45BF_9223_A2D42C74851F&WidgetParams=action%3DgetContent%26blockGUID%3D%257BC99ECD05-3D48-4C62-ABF0-66292053AED6%257D%26eventName%3DRequestFullfimentItem%26filterGUID%3D%257B096C2D42-0DF7-4A54-9978-D13E59BAB8ED%257D%26internalGUID%3D%257B78A562B7-B6EA-45BF-9223-A2D42C74851F%257D%26title%3DPedidos%2Bde%2BServi%25C3%25A7o%26maxLines%3D5%26queryGUID%3D%257B78A562B7-B6EA-45BF-9223-A2D42C74851F%257D%26reportGUID%3D%257BC910B0A9-D202-42D4-8E75-CC48FC3EA1D8%257D%26searchText%3DS1234567%26searchGUID%3D%257B78A562B7-B6EA-45BF-9223-A2D42C74851F%257D%26themeGUID%3D%257BC9F11D78-448E-4EAC-A766-C904ED98458E%257D";
$html_bef_ticket = get($url_bef_t);
preg_match('/<a class="aImg" href="(.*)"/U', $html_bef_ticket, $direct_link_tt);
$html_tt = get($direct_link_tt[1]);
print_r($html_tt);
$url_bef_t = "https://example.com/ajax_widget.php?session_id=".$phpSessionEV[1]."&uniqid=".$uniqid[1]."&lng=PO&theme={C9F11D78-448E-4EAC-A766-C904ED98458E}&widgetname=InternalFilteredQuery.InternalFilteredQueryWidget&widgetid=Grid_78A562B7_B6EA_45BF_9223_A2D42C74851F&WidgetParams=action%3DgetContent%26blockGUID%3D%257BC99ECD05-3D48-4C62-ABF0-66292053AED6%257D%26eventName%3DRequestFullfimentItem%26filterGUID%3D%257B096C2D42-0DF7-4A54-9978-D13E59BAB8ED%257D%26internalGUID%3D%257B78A562B7-B6EA-45BF-9223-A2D42C74851F%257D%26title%3DPedidos%2Bde%2BServi%25C3%25A7o%26maxLines%3D5%26queryGUID%3D%257B78A562B7-B6EA-45BF-9223-A2D42C74851F%257D%26reportGUID%3D%257BC910B0A9-D202-42D4-8E75-CC48FC3EA1D8%257D%26searchText%3DS7654321%26searchGUID%3D%257B78A562B7-B6EA-45BF-9223-A2D42C74851F%257D%26themeGUID%3D%257BC9F11D78-448E-4EAC-A766-C904ED98458E%257D";
$html_bef_ticket = get($url_bef_t);
preg_match('/<a class="aImg" href="(.*)"/U', $html_bef_ticket, $direct_link_tt);
$html_tt = get($direct_link_tt[1]);
print_r($html_tt);
//option 2 end
}
main();
What i'm doing wrong?
I think you need to initialize both curl request with two different variables.
function login($user, $pass, $account) {
$URL = 'https://example.com/index.php';
global $cookie_path;
$ch = curl_init($URL);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'url_login='.$user.'&url_password='.$pass.'&url_account='.$account);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_path);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_path);
$ret = curl_exec($ch);
curl_close($ch);
return $ret;
}
function get($url) {
global $cookie_path;
$ch2 = curl_init();
curl_setopt($ch2, CURLOPT_URL, $url);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch2, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch2, CURLOPT_COOKIEJAR, $cookie_path);
curl_setopt($ch2, CURLOPT_COOKIEFILE, $cookie_path);
$html = curl_exec($ch2);
curl_close($ch2);
return $html;
}
I hope this helps you.

looping a curl data using php

need your help... how can I convert it on looping:
//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);
for example there's a specific number count to loop like 4 or more numbers...
then i would like to get all the $chats... and merge..
like this:
$merge_array = array_merge_recursive($chats1, $chats2, $chats3, $chats4);
hope you can help me guy's thanks...
Try to do it like this:
$chats = [];
for($i=1; $i<= $n; $i++){
$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);
$chats[] = json_decode($output1,true);
}
$merge_array = call_user_func_array('array_merge_recursive', $chats);
echo json_encode($merge_array);
Hope this is what you need.

Curl not json response in php

not getting response using curl. I have put all solution but did not get response.
$ch = curl_init();
$header = array('api_key:xxxxxxxxxxxxxxxx','Content-Type: application/json');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
if($postdata!=""){
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
}
$response = curl_exec($ch);
curl_close($ch);
$result = json_decode($response,true);
print_r($result); // not display result
this example not displaying any result but it send to specific place.
Add this in your code and then check.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
For your used-case you have to change your code.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://api.nhs.uk/organisations/FNM60");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$result = json_decode($response,true);
print_r($result);
here we are getting response without echo on the top of the page but we don't need to that type of out put so we have disabled using span tag.
echo "<span style='display:none;'>"; //to hide the curl response
$ch = curl_init();
$header = array('api_key:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx','Content-Type: application/json');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
if($postdata!=""){
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
}
$response = curl_exec($ch);
$info = curl_getinfo($ch);
$responseBody = json_decode($response);
print_r($responseBody);
echo $response; // display 1(one)
curl_close($ch);
echo "</span>";
and result is : 1
check full code

How to debug a get request in php using curl

I'm trying to make a get request in php using curl. This is what I'm doing:
$curl = curl_init();
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "username:password");
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
printf($result);
But $result doesn't print out anything, no success or failure message. I've successfully reached the endpoint via postman and in a web browser so I know it works. Printing out $curl prints: "Resource #1" which makes me think curl is properly installed on the server.
I'm not sure what steps to take next to make things work.
Add a few more option for troubleshooting purposes.
Check for an error response.
If no error, get the details:
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT,10);
curl_setopt($ch, CURLOPT_FAILONERROR,true);
curl_setopt($ch, CURLOPT_ENCODING,"");
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_HEADER, true);
$data = curl_exec($ch);
if (curl_errno($ch)){
$data .= 'Retreive Base Page Error: ' . curl_error($ch);
}
else {
$skip = intval(curl_getinfo($ch, CURLINFO_HEADER_SIZE));
$head = substr($data,0,$skip);
$data = substr($data,$skip);
$info = curl_getinfo($ch);
$info = var_export($info,true);
}
echo $head;
echo $info;
You can utilize curl's CURLOPT_VERBOSE and CURLOPT_STDERR like this:
$curl = curl_init();
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "username:password");
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$verbose = fopen('php://temp', 'w+');
curl_setopt($curl, CURLOPT_VERBOSE, true);
curl_setopt($curl, CURLOPT_STDERR, $verbose);
$result = curl_exec($curl);
curl_close($curl);
rewind($verbose);
$verboseLog = stream_get_contents($verbose);
echo "Verbose information:\n<pre>", htmlspecialchars($verboseLog), "</pre>\n";
printf($result);

htmlspecialchars doesn't work on string

When I use cURL to get a website and do strlen() on the result I get 72354. But when I pass the result through htmlspecialchars() and then do strlen() I get 0. Any ideas? Here's my code:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.google.com/search?q=test');
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 1);
$result = curl_exec($ch);
echo strlen($result);
echo '<br>';
echo strlen(htmlspecialchars($result));
try
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.google.com/search?q=test');
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 1);
$result = curl_exec($ch);
echo strlen($result);
echo '<br>';
echo strlen(htmlspecialchars($result, ENT_QUOTES));
echo '<br>';
echo htmlspecialchars($result, ENT_COMPAT,'ISO-8859-1', true);
?>

Categories