domingo, 10 de noviembre de 2019

object to array

217
The easiest way is to JSON-encode your object and then decode it back to an array:
$array = json_decode(json_encode($object), True);
Or if you prefer, you can traverse the object manually, too:
foreach ($object as $value) 
    $array[] = $value->post_id
$info=json_decode($reply);
$array = json_decode(json_encode($info), True);

//print_r($array);
foreach($array['result'] as $key=>$value) {
echo " $key : $value<br>";
}



Curl request using get and JSON and a Custom header

curl  -H "Content-Type: application/json"  -H "apikey:3ac" -G -d "telephone=100&call_id=555" http://api.dev.com



we use the  H  parameter  multiples time for more than 1 header

martes, 5 de noviembre de 2019

mult function to clean string

<?php

$agent_audio="Local/101@from-queue/n";

echo $d= str_replace('/','',stristr(stristr($agent_audio, '@', true),'/'));
echo "<br>"

?>

will  print  101

martes, 17 de septiembre de 2019

PHP Curl GET Method and POST Method

Requirements

To be able to use Curl you must install on server libcurl package. Depending on what PHP version you have on server you need to install the appropriate libcurl version like:
  • PHP 4.2.3 requires libcurl version 7.9.0
  • PHP 4.3.0 requires librucl version 7.9.8
  • PHP 5.0.0 requires a librucl version 7.10.5

PHP Curl GET Method

Below is simple function for sending GET request using Curl. We send search request to Google and get as return matching results.
  1. $url = 'https://www.google.com/search';
  2.     $data = array (
  3.         'q' => 'nokia'
  4.         );
  5.        
  6.         $params = '';
  7.     foreach($data as $key=>$value)
  8.                 $params .= $key.'='.$value.'&';
  9.          
  10.         $params = trim($params, '&');
  11.     $ch = curl_init();
  12.     curl_setopt($ch, CURLOPT_URL, $url.'?'.$params ); //Url together with parameters
  13.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Return data instead printing directly in Browser
  14.     curl_setopt($ch, CURLOPT_CONNECTTIMEOUT , 7); //Timeout after 7 seconds
  15.     curl_setopt($ch, CURLOPT_USERAGENT , "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
  16.     curl_setopt($ch, CURLOPT_HEADER, 0);
  17.    
  18.         $result = curl_exec($ch);
  19.     curl_close($ch);
  20. if(curl_errno($ch))  //catch if curl error exists and show it
  21.   echo 'Curl error: ' . curl_error($ch);
  22. else
  23.   echo $result;
In '$data' array we can have as many parameters as we want, in our case only 1 is needed. Let's analyze the options in this request: curl_setopt($ch, CURLOPT_URL, $url.'?'.$params ) - Remote URL where we want to send the request curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1) - To store the result in variable you must set this option to 1 or true because if you don't the results will be printed directly in browser. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT , 7)- Using timeout we protect our server from performing long requests if something goes wrong. curl_setopt($ch, CURLOPT_HEADER, 0) - Because we store our result in a variable we set this option to 0 because we don't want also to get the header information that we receive.

PHP Curl POST Method

To send POST request using curl to specific remote location we will modify above code and add extra curl options.
  1. $url = 'http://www.domain.com';
  2.     $data = array (
  3.         'key1' => 'value1',
  4.         'key2' => 'value2',
  5.         'key3' => 'value3'
  6.         );
  7.        
  8.         $params = '';
  9.     foreach($data as $key=>$value)
  10.                 $params .= $key.'='.$value.'&';
  11.          
  12.         $params = trim($params, '&');
  13.     $ch = curl_init();
  14.     curl_setopt($ch, CURLOPT_URL, $url); //Remote Location URL
  15.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Return data instead printing directly in Browser
  16.     curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 7); //Timeout after 7 seconds
  17.     curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
  18.     curl_setopt($ch, CURLOPT_HEADER, 0);
  19.        
  20.     //We add these 2 lines to create POST request
  21.     curl_setopt($ch, CURLOPT_POST, count($data)); //number of parameters sent
  22.     curl_setopt($ch, CURLOPT_POSTFIELDS, $params); //parameters data
  23.    
  24.     $result = curl_exec($ch);
  25.     curl_close($ch);
  26.     echo $result;
As you can see from previous GET Request php code there are 2 more lines in above code: curl_setopt($ch, CURLOPT_POST, count($data)) - We set the number of posted parameters curl_setopt($ch, CURLOPT_POSTFIELDS, $params) - We add here the POST parameters separately from the main url.

Other Posts Yo

miércoles, 21 de agosto de 2019

Ignore sqlsrv error

Array ( [0] => Array ( [0] => 01000 [SQLSTATE] => 01000 [1] => 0 [code] => 0 [2] => [Microsoft][SQL Server Native Client 10.0][SQL Server]We Will Not Show Clicks,CTR and RPC Data For This Partner. [message] => [Microsoft][SQL Server Native Client 10.0][SQL Server]We Will Not Show Clicks,CTR and RPC Data For This Partner. ) )

When a message, e.g. from a SQL 'print' statement is executed, the sqlsrv driver forwards it through sqlsrv_errors().
The default setting is to treat all warnings as errors, so your query will fail...
Set the feature to treat warning as errors to false like this, so that the messages will not fail your query:
sqlsrv_configure('WarningsReturnAsErrors',0);
Also, don't forget to call sqlsrv_next_result($yourStmt); to scroll through all of the warnings and results.  When sqlsrv_next_result() returns NULL, you have reached the end of all results without any errors.

domingo, 18 de agosto de 2019

passing a function results as argument to other function

<?php
$string="0:1027301|pe_id:1027301|1:ko ben|NAME:ko
 ben|2:0555532963|PHONE2:0555532963|3:07711111|PHONE1:07711111|";

echo $value=preg_replace('/[^0-9]+/', '', stristr(stristr($string, 'PHONE2'),'|',true));

?>
 we use 3 functions  (  2 times stristr()  and preg_replace()

search value of PHONE2:0555532963


sábado, 17 de agosto de 2019

php imap

<?php
/* connect to gmail */

$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'oro14k1@gmail.com';
$password = '12231';

/* try to connect */
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());

/* grab emails */
$emails = imap_search($inbox,'ALL');

/* if emails are returned, cycle through each... */
if($emails) {
 
 /* begin output var */
 $output = '';
 
 /* put the newest emails on top */
 rsort($emails);
 
 /* for every email... */
 foreach($emails as $email_number) {
  
  /* get information specific to this email */
  $overview = imap_fetch_overview($inbox,$email_number,0);
  $message = imap_fetchbody($inbox,$email_number,2);
  
  /* output the email header information */
  $output.= '<div class="toggler '.($overview[0]->seen ? 'read' : 'unread').'">';
  $output.= '<span class="subject">'.$overview[0]->subject.'</span> ';
  $output.= '<span class="from">'.$overview[0]->from.'</span>';
  $output.= '<span class="date">on '.$overview[0]->date.'</span>';
  $output.= '</div>';
  
  /* output the email body */
  $output.= '<div class="body">'.$message.'</div>';
 }
 
 echo imap_qprint($output);
} 

/* close the connection */
imap_close($inbox);

?>

viernes, 2 de agosto de 2019

php split

<?php

function clean($data){
$data=preg_replace('/[^0-9]+/', '', $data);
$data = str_split($data);
return $data;
}


$a=clean("812z5124zdd346");

print_r($a[0]);

print_r($a[1]);

print_r($a[2]);


this will only allow numbes and  create  an arrary from the string,   using function mode

php convert string to array str_split()

string
The input string.
split_length
Maximum length of the chunk.

Return Values ¶

If the optional split_length parameter is specified, the returned array will be broken down into chunks with each being split_length in length, otherwise each chunk will be one character in length.
FALSE is returned if split_length is less than 1. If the split_length length exceeds the length of string, the entire string is returned as the first (and only) array element.

Examples ¶

Example #1 Example uses of str_split()
<?php

$str 
"Hello Friend";
$arr1 str_split($str);$arr2 str_split($str3);
print_r($arr1);print_r($arr2);
?>
The above example will output:
Array
(
    [0] => H
    [1] => e
    [2] => l
    [3] => l
    [4] => o
    [5] =>
    [6] => F
    [7] => r
    [8] => i
    [9] => e
    [10] => n
    [11] => d
)

Array
(
    [0] => Hel
    [1] => lo
    [2] => Fri
    [3] => end
)

lunes, 29 de julio de 2019

php imap

https://www.electrictoolbox.com/php-imap-message-body-attachments/

install imap php

sudo yum install php-imap
aptitude install php5-imap
php.ini file
[imap] ; rsh/ssh logins are disabled by default. Use this INI entry if you want to ; enable them. Note that the IMAP library does not filter mailbox names before ; passing them to rsh/ssh command, thus passing untrusted data to this function ; with rsh/ssh enabled is insecure. imap.enable_insecure_rsh=1

displaying gmail using imap

/* connect to gmail */
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'davidwalshblog@gmail.com';
$password = 'davidwalsh';

/* try to connect */
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());

/* grab emails */
$emails = imap_search($inbox,'ALL');

/* if emails are returned, cycle through each... */
if($emails) {
 
 /* begin output var */
 $output = '';
 
 /* put the newest emails on top */
 rsort($emails);
 
 /* for every email... */
 foreach($emails as $email_number) {
  
  /* get information specific to this email */
  $overview = imap_fetch_overview($inbox,$email_number,0);
  $message = imap_fetchbody($inbox,$email_number,2);
  
  /* output the email header information */
  $output.= '<div class="toggler '.($overview[0]->seen ? 'read' : 'unread').'">';
  $output.= '<span class="subject">'.$overview[0]->subject.'</span> ';
  $output.= '<span class="from">'.$overview[0]->from.'</span>';
  $output.= '<span class="date">on '.$overview[0]->date.'</span>';
  $output.= '</div>';
  
  /* output the email body */
  $output.= '<div class="body">'.$message.'</div>';
 }
 
         echo imap_qprint($output);

} 

/* close the connection */
imap_close($inbox);
https://davidwalsh.name/gmail-php-imap