Facebook api , Recursive Function and 500 error - php

this is my first project using facebook api and facebook PHP sdk , basically i'm trying to get all user statuses. I wrote a script that should work , but i got an 500 error (even if i changed max execution times or set time limit (0)), but only when i use a recursive function inside, take a look to the code :
$request = new FacebookRequest(
$session,
'GET',
'/me/statuses'
);
$response = $request->execute();
$graphObject = $response->getGraphObject();
$x = $graphObject->getProperty('data');
$y = $x->asArray(); //now i got an array
$paging = $graphObject->getProperty('paging'); // i pick paging with "next" and "prevoiuos "
$paged = $paging->asArray(); //as array
$counter = 0 ;
foreach ($y as $el){
echo ('<h3>'.$y[$counter]->message.'</h3>');
echo "<br/>";
echo "<br/>";
$counter++;
}
$next = $paged['next']; // now i got url for load 20 more statuses
$response = file_get_contents($next); // get content of url
//recoursive function every time i use looper with content of next
function looper($response){
$array = json_decode($response, true);
$secondarray = ($array['data']);
$paging = ($array['paging']); // again i pick url for load next statuses
$next = $paging['next'];// again i pick url for load next statuses
$nextResponse = file_get_contents($next);// again i pick url for load next statuses and i will use this.
$counter2 = 0 ;
foreach ($secondarray as $el){ // put on page next 20 statuses
echo ('<h3>'. $secondarray[$counter2]['message'] .'</h3>');
echo "<br/>";
echo "<br/>";
$counter2++;
}
if ( is_null($nextResponse) == false ){ // if in next call i got 20 more statuses(not empty) call again this function
looper($nextResponse);
} else { echo "last message" ; die();} //else stop.
}
looper($response);
}
If i dont recall the function (basically i comment out the if statement) script works fine and prints 20+20 statuses , else it give me 500 internal error.
As i said i tried changin max execution time or set_time_limit(0), but nothing happens.
I'm not sure if problem is my hosting (godaddy) , or if my script is not good / not efficent. any help?
Thanks Nico

I think I found your issue. You are asigning $nextResponse the value returned by file_get_contents. See http://php.net/manual/es/function.file-get-contents.php it returns false in case of content couldnt be retrieved. Try checking for false instead of null:
..........
if ( false != $nextResponse ){ // if in next call i got 20 more statuses(not empty) call again this function
looper($nextResponse);
} else { echo "last message" ; die();} //else stop.

Related

Dynamic content, shows a 1 on each request

With my function what I have written I try thereby 2 things.
The links should be called like this http://localhost/?login=Bla, Now it is like this http://localhost/login,php?login "Bla
Next I would have asked, in my function a 1 is given after each call. I just can't figure out where this comes from, I've been sitting on this problem for a long time.
Output with the 1
This is the code with which I can call the pages
function Seite($pagename, $lay){
function Seite($pagename, $lay){
$path = "$lay/$pagename.php";
if (file_exists($path)) {
openSeite($path);
}
}
function openSeite($pageurl){
$fc = require($pageurl);
echo $fc;
}
function echopage($slug, $fade){
// $slug = ?SLUG=Seite
// $fade = Ordner des Layout
$page = isset($_GET["$slug"]) ? $_GET["$slug"] : "error";
$contente = seite($page, "$fade");
echo $contente;
}
I call the content on the index.php with
<? echopage("login", "admin/layout"); ?>
isset($_GET["$slug"]) returns a 1 because it is set (true), write a traditional conditional with the echo inside the if statement.
*Better Yet assign your output to a variable and concatenate the values accordingly.
$output = NULL;
if(isset($_GET["$slug"]){
$contente = seite($page, "$fade");
$output .= $contente;
}else{
//handle error
}
HTML:
<?=$output?><!--Output your displayed text held in the variable-->
ISSUE:
$page = isset($_GET["$slug"]) ? $_GET["$slug"] : "error";
You are essentially returning the set value, which is 1 also true.
From php manual for value: Returns TRUE if var exists and has any value other than NULL. FALSE otherwise.
You can test this by simply writing out a line of code echo isset($var); and checking the test php page. Then try defining a variable and doing the same thing. $var = "this is set"; then echo isset($var);, you will get a 1.

PHP waiting for correct response with cURL

I don't know how to make this.
There is an XML Api server and I'm getting contents with cURL; it works fine. Now I have to call the creditCardPreprocessors state. It has 'in progress state' too and PHP should wait until the progess is finished. I tried already with sleep and other ways, but I can't make it. This is a simplified example variation of what I tried:
function process_state($xml){
if($result = request($xml)){
// It'll return NULL on bad state for example
return $result;
}
sleep(3);
process_state($xml);
}
I know, this can be an infite loop but I've tried to add counting to exit if it reaches five; it won't exit, the server will hang up and I'll have 500 errors for minutes and Apache goes unreachable for that vhost.
EDIT:
Another example
$i = 0;
$card_state = false;
// We're gona assume now the request() turns back NULL if card state is processing TRUE if it's done
while(!$card_state && $i < 10){
$i++;
if($result = request('XML STUFF')){
$card_state = $result;
break;
}
sleep(2);
}
The recursive method you've defined could cause problems depending on the response timing you get back from the server. I think you'd want to use a while loop here. It keeps the requests serialized.
$returnable_responses = array('code1','code2','code3'); // the array of responses that you want the function to stop after receiving
$max_number_of_calls = 5; // or some number
$iterator = 0;
$result = NULL;
while(!in_array($result,$returnable_responses) && ($iterator < $max_number_of_calls)) {
$result = request($xml);
$iterator++;
}

PHP iterate multidimensional array - query web service every x seconds

I have an array with user information and a web service on a site I can query for the status of a user (online/offline). What I would like to do is query the site every x seconds for the status of each user.
There are about 10 users and belwois an example of the array. I can change the array is needed. Only thing I need to enter manually is the username and full name. The "status" I can call from the server.
$users = array
(
"username"=>array("Fullname","Status"),
"johndoe"=>array("John Doe","Online"),
"janedoe"=>array("Jane Doe","Offline")
);
This is an example of the url I can use to query the site (the query returns only the users status (Online or Offline):
http://thesite.com:80/webservice/user/username/
This is the code I can use to get a specific user status:
$url = 'http://thesite.com:80/webservice/user/johndoe/';
$get = fopen($url, "r");
if ($get) {
while (!feof($get)) {
$state = fgets($get, 4096);
}
fclose($get);
}
echo "User johndoe is: ".$status;
// Output: User johndoe is: Online
Now I only need help with iterating through the users and site every x seconds and update the array with each user status in the last array field for the user.
Please note that below I use php and fopen as this is a cross-domain get function and I could not get ajax/jquery to work. I do not have the option to modify the webservice server.
Thanks :)
You need to create a cronjob script that runs every x seconds. That script should be an asynchronous request to this PHP function.
public function updateUsers(){
$users = $_SESSION['users'];
foreach($users as $username=>$data) {
$url = 'http://thesite.com:80/webservice/user/'.$username.'/';
$get = fopen($url, "r");
if ($get) {
while (!feof($get)) {
$status = fgets($get, 4096);
}
fclose($get);
}
$users[$username][] = $status;
}
$_SESSION['users'] = $users;
}
A guide for posting asynchronous requests . http://petewarden.typepad.com/searchbrowser/2008/06/how-to-post-an.html
Hope it helps :)
If your $users array don't changes, you can do this:
foreach($users as $username=>$userdata) {
$url = 'http://thesite.com:80/webservice/user/'.$username.'/';
$get = fopen($url, "r");
if ($get) {
while (!feof($get)) {
$state = fgets($get, 4096);
}
fclose($get);
}
$users[$username][1] = $state;
}
If you can change your $users array to be associative like this:
$users = array(
"username"=>array("fullname"=>"Fullname","status"=>"Status"),
"johndoe"=>array("fullname"=>"John Doe","status"=>"Online"),
"janedoe"=>array("fullname"=>"Jane Doe","status"=>"Offline")
);
That would let you use more key/values and a bit safer.

Twilio REST API sequential Dialling

I really can't figure out why my code isn't working because Twilio's debugger isnt giving me errors so I don't know what to do...I am trying to make Sequential dialing in REST api using Twilio so it should keep calling numbers in order until one person picks up..Below is my code I have written so far. I am using sessions to keep track of the calls.
File Name:dial.php
<?php
session_start();
require 'Services/Twilio.php';
$version = "2010-04-01";
$arr = array('4167641123','6478604321','9058553456');
$sid = '....';
$token = '...';
$from = '....';
$to = '416.....';
$callback = 'www.site.com/dial.php';
$client = new Services_Twilio($sid, $token, $version);
//if this is our very first call then CallStatus should be empty so it means we can use the emptiness of this variable
//to trigger our very first call
if (!(isset($_REQUEST['CallStatus'])))
{
try {
$call = $client->account->calls->create(
$from,
$arr[0],
'http://demo.twilio.com/welcome/voice/',
array('Timeout' => 1,
'StatusCallback' => $callback)
);
var_dump($call);
} catch (Exception $e) {
var_dump($e);
}
}
// if the CallStatus variable is not empty then the else statement will execute
else
{
//if this part of code runs for the first time, it means this is our 2nd call because the 1st person did not pick up
//this means the second number in the array will be called
//each time this statement runs it adds a 1 to the index of the array but if the last index number called was the final and
//last number in the array, then this statement wont run and instead session at the bottom if statement will be initialized to 0
//so that if this script is ran again it will start off from the first number in the array
if (!($_SESSION['X']>=count($arr)-1) && isset($_REQUEST['CallStatus']) && ($_REQUEST['CallStatus']=='failed'|| $_REQUEST['CallStatus']=='no-answer' || $_REQUEST['CallStatus']=='busy'))
{
$_SESSION['X']=$_SESSION['X']+1;
try {
$call = $client->account->calls->create(
$from,
$arr[SESSION['X']],
'http://demo.twilio.com/welcome/voice/',
array('Timeout' => 1,
'StatusCallback' => $callback)
);
var_dump($call);
} catch (Exception $e) {
var_dump($e);
}
}
//initializes the session to 0 because if we have reached to this else statement,
//then it means the if statement above did not run and we have already called the last person
//in the phone number array so we are at an end and we must close the program
//by leaving session at 0 for the next trial to run properly
else
{
$_SESSION['X']=0;
}
}
I have this other file same, sequential diallnig written using php and twilio's Dial verb, the Dial verb's parameters allow it to pass the array index but in my case I don't know how to pass the array index parameter....any ideas?
<?php
// Set the numbers to call
$numbers = array("<number to call 1>", "<number to call 2>", "<number to call n>");
$number_index = isset($_REQUEST['number_index']) ? $_REQUEST['number_index'] : "0";
$DialCallStatus = isset($_REQUEST['DialCallStatus']) ? $_REQUEST['DialCallStatus'] : "";
header("content-type: text/xml");
// Check the status of the call and
// that there is a valid number to call
if($DialCallStatus!="completed" && $number_index<count($numbers)){
?>
<Response>
<Dial action="attempt_call.php?number_index=<?php echo $number_index+1 ?>">
<Number url="screen_for_machine.php">
<?php echo $numbers[$number_index] ?>
</Number>
</Dial>
</Response>
<?php
} else {
?>
<Response>
<Hangup/>
</Response>
<?php
}
?>
Your code has syntax errors and such.
For example SESSION['X']=0; is not doing what you might have thought it is doing. If that's a session variable, it should be written as $_SESSION['X']=0;.
To spot these issues, enable error reporting to the highest level, then log errors to a file and then watch that file. It will give you some hints.
See Error Handling and Logging Docs.

PHP curl questions - running multiple times

I have this code:
<?php
foreach($items as $item) {
$site = $item['link'];
$id = $item['id'];
$newdata = $item['data_a'];
$newdata2 = $item['data_b'];
$ch = curl_init($site.'updateme.php?id='.$id.'&data1='.$newdata.'&data2='.$newdata2);
curl_exec ($ch);
// do some checking here
curl_close ($ch);
}
?>
Sample input:
$site = 'http://www.mysite.com/folder1/folder2/';
$id = 512522;
$newdata = 'Short string here';
$newdata = 'Another short string here with numbers';
Here the main process of updateme.php
if (!$id = intval(Tools::getValue('id')))
$this->_errors[] = Tools::displayError('Invalid ID!');
else
{
$history = new History();
$history->id = $id;
$history->changeState($newdata1, intval($id));
$history->id_employee = intval($employee->id_employee);
$carrier = new Carrier(intval($info->id_carrier), intval($info->id_lang));
$templateVars = array('{delivery}' => ($history->id_data_state == _READY_TO_SEND AND $info->shipping_number) ? str_replace('#', $info->shipping_number, $carrier->url) : '');
if (!$history->addWithemail(true, $templateVars))
$this->_errors[] = Tools::displayError('an error occurred while changing status or was unable to send e-mail to the employee');
}
The site will always be changing and each $items will have atleast 20 data inside it so the foreach loop will run atleast 20 times or more depending on the number of data.
The target site will update it's database with the passed variables, it will probably pass thru atleast 5 functions before it is saved to the DB so it could probably take some time too.
My question is will there be a problem with this approach? Will the script encounter a timeout error while going thru the curl process? How about if the $items data is around 50 or in the hundreds now?
Or is there a better way to do this?
UPDATES:
* Added updateme.php main process code. Additional info: updateme.php will also send an email depending on the variables passed.
Right now all of the other site are hosted in the same server.
You can have a php execution time problem.
For your curl timeout problem, you can "fix" it using the option CURLOPT_TIMEOUT.
Since the cURL script that calls updateme.php doesn't expect a response, you should make updateme.php return early.
http://gr.php.net/register_shutdown_function
function shutdown() {
if (!$id = intval(Tools::getValue('id')))
$this->_errors[] = Tools::displayError('Invalid ID!');
else
{
$history = new History();
$history->id = $id;
$history->changeState($newdata1, intval($id));
$history->id_employee = intval($employee->id_employee);
$carrier = new Carrier(intval($info->id_carrier), intval($info->id_lang));
$templateVars = array('{delivery}' => ($history->id_data_state == _READY_TO_SEND AND $info->shipping_number) ? str_replace('#', $info->shipping_number, $carrier->url) : '');
if (!$history->addWithemail(true, $templateVars))
$this->_errors[] = Tools::displayError('an error occurred while changing status or was unable to send e-mail to the employee');
}
}
register_shutdown_function('shutdown');
exit();
You can use set_time_limit(0) (0 means no time limit) to change the timeout of the PHP script execution. CURLOPT_TIMEOUT is the cURL option for setting the timeout, but I think it's unlimited by default, so you don't need to set this option on your handle.

Categories