when user allow permission & script start processing every data is shown perfectly but i get error
PHP Notice: Undefined index: gd$email in
here is my php code
if (!empty($contacts['feed']['entry']))
{
foreach($contacts['feed']['entry'] as $contact)
{
// retrieve user photo
if (isset($contact['link'][0]['href']))
{
$url = $contact['link'][0]['href'];
$url = $url . '&access_token=' . urlencode($accesstoken);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_TIMEOUT, 15);
curl_setopt($curl, CURLOPT_VERBOSE, true);
$image = curl_exec($curl);
curl_close($curl);
}
if ($image === 'Photo not found')
{
// retrieve Name and email address
$return[] = array(
'name' => $contact['title']['$t'],
'email' => $contact['gd$email'][0]['address'],
'img_url' => '//cdn.twkcdn.com/profile/image/avatar.png?w=40&h=40&cf',
);
}
else
{
// retrieve Name and email address
$return[] = array(
'name' => $contact['title']['$t'],
'email' => $contact['gd$email'][0]['address'],
'img_url' => $url,
);
}
}
$google_contacts = $return; //returning all d
}
This is just example of half script & i don't know why fetching contacts from google take about 15 to 20 seconds everytime i visit this page
Google doesn't check that all entry has email address.
Add that immediatly after Foreach :
if (!array_key_exists('gd$email', $contact)){
continue;
}
Related
in function input $files = $_FILES
Don't get what Telegram wants from me.
It says: "{"ok":false,"error_code":400,"description":"Bad Request: group send failed"}". HIELPLEAS!
function sendMediaGroup($files)
{
$url = "https://api.telegram.org/bot" . $this->token . "/" . __FUNCTION__;
$media = [];
$ch = curl_init();
$type = "photo";
$caption = "";
foreach ($files as $file)
{
$media[] = [
'type' => $type,
'media' => $file['tmp_name'],
'caption' => $caption
];
}
$disable_notification = false;
$reply_to_message_id = null;
$parameters = [
'chat_id' => $this->chat_id,
'media' => json_encode($media),
'disable_notification' => $disable_notification,
'reply_to_message_id' => $reply_to_message_id,
];
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:multipart/form-data"));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
return $output = curl_exec($ch);
}
In order for Telegram to create a media group from photo URL in the Internet, a file link you pass as media attribute value must be accessible by Telegram's servers. Sometimes it is not the case and sendMediaGroup API call fails with the cryptic error message Bad Request: group send failed. In this case you can try another approach to send photos, e.g. in text field using sendMessage method or go to #webpagebot and send him a link to your file - this will update preview for the link and you will be able to send that link inside of the media group.
Note to moderators: this answer does not 100% targets original question, but link to this question pops up in first ten while searching using words from the caption.
You should name your files and attach files to the request according their name. So change your code like this:
function sendMediaGroup($files)
{
$url = "https://api.telegram.org/bot" . $this->token . "/" . __FUNCTION__;
$media = [];
$ch = curl_init();
$type = "photo";
$caption = "";
$parameters = array();
foreach ($files as $file)
{
$media[] = [
'type' => $type,
'media' => "attach://" . $file['tmp_name'],
'caption' => $caption
];
$parameters[] = [
$file['tmp_name'] => $file
];
}
$disable_notification = false;
$reply_to_message_id = null;
$parameters[] = [
'chat_id' => $this->chat_id,
'media' => json_encode($media),
'disable_notification' => $disable_notification,
'reply_to_message_id' => $reply_to_message_id,
];
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:multipart/form-data"));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
return $output = curl_exec($ch);
}
If you are setting URL for media, you URL should work on :80 or :443 port. For example: https:examplesite.com/image1.jpg is OK, https:examplesite.com:8443/image1.jpg is not OK!
I'm trying to create a variable and place it in an array. Like this:
$ch = curl_init('https://apps.net-results.com/api/v2/rpc/server.php?Controller=Contact');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, 'user:pass');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,
json_encode(
array(
'id' => uniqid(),
'method' => 'getMultiple',
'jsonrpc' => '2.0',
'params' => array(
'offset' => 0,
'limit' => 50, // 10, 25, or 50
'order_by' => 'contact_email_address', //'contact_email_address' or 'contact_id'
'order_dir' => 'ASC', //'ASC' or 'DESC'
)
)
)
);
$strResponse = curl_exec($ch);
$strCurlError = curl_error($ch);
if (!empty($strCurlError)) {
//handle curl error
echo "Curl Error<br />$strCurlError<br />";
} else {
//check for bad user/pass
if ($strResponse == "HTTP/1.0 401 Unauthorized: Your username name and/or password are invalid.") {
//handle login error
echo "Error<br />$strResponse<br />";
} else {
//successful call, check for api success or error
$objResponse = json_decode($strResponse);
if (property_exists($objResponse, 'error') && !is_null($objResponse->error)) {
//handle error
$intErrorCode = $objResponse->error->code;
$strMessage = $objResponse->error->message;
$strData = $objResponse->error->data;
echo "Error<br />Code: $intErrorCode<br />Message: $strMessage<br />Data: $strData<br />";
} else {
//handle success
//echo "Success<br />";
$objResult = $objResponse->result;
$intTotalRecords = $objResult->totalRecords;
//echo "Total Records: $intTotalRecords<br />";
$arrContacts = $objResult->results;
//echo $arrContacts[0]->country;
//echo $arrContacts[3]->last_name;
//echo "<pre>";
//print_r($arrContacts);
//echo "<pre/>";
}
}
}
I'm not sure this is possible. I have tried doing different things such as creating a class, and placing the function in the class in the array, but it doesn't work. Can anyone give me the correct syntax of what I should try?
If you pass params via post you should use http_build_query() and you can pass it as CURL post fields.
You should set:
$array = [
'id' => uniqid(),
'method' => 'getContactActivity',
'jsonrpc' => '2.0'
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://example.site");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($array));
curl_exec($ch);
curl_close($ch);
Leave out the call to json_encode(). The value of the CURLOPT_POSTFIELDS option should be either an associative array or a string in URL-encoded format. If you use an array, it will automatically encode it for you.
I'm a beginner in PHP, so maybe someone could help to fix this ?
My web application is showing Google PageInsights API error..
Here's the code, I tried to change version to /v2/, but it still didn't work..
public function getPageSpeed($domain, $api = "")
{
try
{
$callback_url = "https://www.googleapis.com/pagespeedonline/v1/runPagespeed?";
$data = array(
'url' => 'http://' . $domain,
'key' => (empty($api) ? $_SESSION['GOOGLEAPI_SERVERKEY'] : $api),
'fields' => 'score,pageStats(htmlResponseBytes,textResponseBytes,cssResponseBytes,imageResponseBytes,javascriptResponseBytes,flashResponseBytes,otherResponseBytes)'
);
$curl_response = $this->curl->get($callback_url . http_build_query($data, '', '&'));
if ($curl_response->headers['Status-Code'] == "200") {
$content = json_decode($curl_response, true);
$response = array(
'status' => 'success',
'data' => array(
'pagespeed_score' => (int)$content['score'],
'pagespeed_stats' => $content['pageStats']
)
);
} else {
$response = array(
'status' => 'error',
'msg' => 'Google API Error. HTTP Code: ' . $curl_response->headers['Status-Code']
);
}
}
catch (Exception $e)
{
$response = array(
'status' => 'error',
'msg' => $e->getMessage()
);
}
return $response;
}
<?php
function checkPageSpeed($url){
if (function_exists('file_get_contents')) {
$result = #file_get_contents($url);
}
if ($result == '') {
$ch = curl_init();
$timeout = 60;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$result = curl_exec($ch);
curl_close($ch);
}
return $result;
}
$myKEY = "your_key";
$url = "http://kingsquote.com";
$url_req = 'https://www.googleapis.com/pagespeedonline/v1/runPagespeed?url='.$url.'&screenshot=true&key='.$myKEY;
$results = checkPageSpeed($url_req);
echo '<pre>';
print_r(json_decode($results,true));
echo '</pre>';
?>
The code shared by Siren Brown is absolutely correct, except that
while getting the scores we need to send the query parameter &strategy=mobile or &strategy=desktop to get the respective results from Page speed API
$url_mobile = 'https://www.googleapis.com/pagespeedonline/v1/runPagespeed?url='.$url.'&screenshot=true&key='.$myKEY.'&strategy=mobile';
$url_desktop = 'https://www.googleapis.com/pagespeedonline/v1/runPagespeed?url='.$url.'&screenshot=true&key='.$myKEY.'&strategy=desktop';
I am sending a GCM push message (less then 1000) with:
$Regids = mysql_query("SELECT regid FROM $tabel WHERE active = '1'");
$result_array = array();
while ($row = mysql_fetch_array($Regids)){
$result_array[] = $row['regid'];
}
$headers = array(
'Content-Type:application/json',
'Authorization:key=' . $serverApiKey
);
$data = array(
'registration_ids' => $result_array,
'data' => array(
'type' => 'New',
'title' => 'LH',
'msg' => ''.$msj.''
)
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
if ($headers)
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
$result = curl_exec($ch);
curl_close($ch);
When I print the $result to check (and use for updating the database) it shows:
{"multicast_id":000,"success":274,"failure":75,"canonical_ids":13,"results":[{"message_id":"123"},{"message_id":"123"},{"error":"NotRegistered"},{"error":"NotRegistered"},{"message_id":"123"},{"message_id":"123"},{"registration_id":"456","message_id":"123"}]}
There is no },{ between every (canonical) registration_id and the next (success) message_id or error.
(I shortened the response to make it readable)
So my update part fails
$result = json_decode($result);
for ($i = 0; $i < count($result->{'results'}); $i++) {
if ($result->{'results'}[$i]->{'message_id'}) {
...
} elseif($result->{'results'}[$i]->{'error'}) {
...
} elseif ($result->{'results'}[$i]->{'registration_id'}) {
...
}
}
Does someone know what's wrong?
The format of the response you got is correct . A message for which you get a canonical registration id is still accepted by Google and is therefore successful , and that's why you get a message id in addition to the canonical registration id for that message . That's why there are no brackets after the canonical registration id.
I am using PHP. I want to fetch all gmail contacts of a user, i am using a PHP code that is calling google API through CURL. But, when i am doing this on localhost, it is doing well and giving me all contacts.
But when i am doing this on online server that server in US, it is giving me response "Account Disabled" and also receiving a security mail by same user from google.
i am using below code :
function getGmailContacts($user, $password) {
//========================================== step 1: login ===========================================================
$login_url = "https://www.google.com/accounts/ClientLogin";
$fields = array(
'Email' => $user,
'Passwd' => $password,
'service' => 'cp', // <== contact list service code
'source' => 'test-google-contact-grabber',
'accountType' => 'GOOGLE',
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,$login_url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS,$fields);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
$returns = array();
foreach (explode("\n",$result) as $line)
{
$line = trim($line);
if (!$line) continue;
list($k,$v) = explode("=",$line,2);
$returns[$k] = $v;
}
curl_close($curl);
//echo "<pre>";
//print_r($returns);exit;
if(!isset($returns['Error'])) {
//========================== step 2: grab the contact list ===========================================================
$feed_url = "http://www.google.com/m8/feeds/contacts/$user/full?alt=json&max-results=250";
$header = array(
'Authorization: GoogleLogin auth=' . $returns['Auth'],
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $feed_url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
$data = json_decode($result, true);
//echo "<pre>";
//print_r($data);exit;
$contacts = array();
$i=0;
foreach ($data['feed']['entry'] as $entry)
{
//echo $i." ";
$entryElement = $entry;
if(isset($entryElement['gd$email'])) {
$gdEmailData = $entryElement['gd$email'][0];
//$contact->title = $entryElement['title']['$t'];
//$contact->email = $gdEmailData['address'];
$contacts[$gdEmailData['address']] = $entryElement['title']['$t'];
}
}
//var_dump($contacts);
//print_r($contacts);
return $contacts;
}
else {
if($returns['Error']=='BadAuthentication') {
//echo '<strong>User Name and Password is incorrect.</strong>';
$errorArr = array("Error"=>"User Name and Password is incorrect.");
//print_r($errorArr);
return $errorArr;
}
}
}
That mail contains that
"Someone recently tried to use an application to sign in to your
Google Account....Location: New York NY, New York, United States....."
.
I thing this error is occurring from location change.
can any one help me please? Thanks in advance.