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.
Related
I have searched many previous posts here, and nothing seems to work with my issue. In a nutshell, I am looking to include a PHP variable in the following code. This code below works as it is hardcoded, but I get an error when I replace the word MAYIAHH with a variable $hid (which has been declared).
I have tried CURLOPT_POSTFIELDS and all sorts, but nothing seems to help. Any ideas, please?
function send_request($xml)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,
'https://rest.reserve-online.net/availability?properties=MAYIAHH');
curl_setopt($ch, CURLOPT_USERPWD, "uname:pass");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
Have you tried to concat the url string?
$hid = 'MAYIAHH';
$url = 'https://rest.reserve-online.net/availability?properties='.$hid;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "uname:pass");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
return $result;
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
}
This times out every time. How to fix it?
<?
$curl = curl_init("https://gstdealerdaily.fitsvcs.com/siteminderagent/forms/login.fcc");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
print $result;
You need to add more cURL functions in order to work.
<?php
$target_url="https://gstdealerdaily.fitsvcs.com/siteminderagent/forms/login.fcc";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,4);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$html = curl_exec($ch);
$html = preg_replace("#(<\s*a\s+[^>]*href\s*=\s*[\"'])(?!http)([^\"'>]+) ([\"'>]+)#",'$1'.$target_url.'$2$3', $html);
echo $html;
curl_close($ch);
var_dump($ch);
?>
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);
?>
I am trying to fetch a json string from a password protected url.
I think my code should work but when I open my script in the browser I just have a blank page.
Here's my code:
<?php
error_reporting(E_ALL);
$url = 'https://api.domain.com?api=latest&format=json';
$username = 'username';
$password = 'password';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
//curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
//curl_setopt($ch, CURLOPT_REFERER, $url);
$result = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
$data = json_decode($result, true);
print_r($data);
?>
Any help would be much appreciated.
Try adding this
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_UNRESTRICTED_AUTH, 1);