How to block an infinite-loop process during PHP web service? - php

I made a mistake during PHP web-service development.
The problem is that web service re-call itself by curl without return anything.
My mystake example code exposed into server at the following endpoint:
/server/get-my-infinite-loop-error
public function myInfiniteLoopError(){
$url = '/server/get-my-infinite-loop-error' // my web-call mistake
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($curl);
// never code executed right?
curl_close($curl);
$data = json_decode($result);
// json response
echo $data;
}
Now i am still stuck since almost an hour.
Do you have any idea to face the problem?
Thanks a lot in advance.

Related

PHP string specialchars

I'm setting up a little webservice usable via an app on Android.
The function in the server is this:
$lat=43.0552592;
$lng=12.4483577;
$radius=2000;
$url = "https://maps.googleapis.com/maps/api/place/textsearch/json?query=italian&location=".$lat.",".$lng."&radius=".$raidus."&types=restaurant&sensor=false&key=".$apiKey;
$urlW = "https://maps.googleapis.com/maps/api/place/textsearch/json?query=italian&location=43.0552592,12.4483577&radius=2000&types=restaurant&sensor=false&key=XXXXxxxXXxxXXxxxxXXX";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
// Set so curl_exec returns the result instead of outputting it.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Get the response and close the channel.
$response = curl_exec($ch);
return $response;
The trouble is that if I query Google with the $url (as in the above example) it returns the JSON with 'INVALID REQUEST', but if the query at the first curl_opt is done with $curlW will works like a charm.
While debugging that I've discovered, making $url return, that gets every & converted (before the curl_init!) in &amp...!
So I've tried almost every PHP string function to force every & decode or replacing every &entities to & only without any result.
Any suggestion?
you've done something wrong,
$url = "https://maps.googleapis.com/maps/api/place/textsearch/json?query=italian&location=".$lat.",".$lng."&radius=".$raidus."&types=restaurant&sensor=false&key=".$apiKey
should be
$url = "https://maps.googleapis.com/maps/api/place/textsearch/json?query=italian&location=".$lat.",".$lng."&radius=".$radius."&types=restaurant&sensor=false&key=".$apiKey;
but i don't guess that is what is doing the problem, try using file_get_contents() instead of curl, so $response will be like this:
$response = file_get_contents($url);

curl_exec not working inside a loop

I have a class with the function below. When I call that function directly it works, but if I call that function inside the loop I got an error: Cannot connect to host. Can somebody help me? :) Thanks in advance.
public function exportProfile($xml) {
$_myRequest = curl_init("https://www.example.com/postProfile");
curl_setopt($_myRequest, CURLOPT_POST, 1);
curl_setopt($_myRequest, CURLOPT_USERPWD, "testUser:testPassword");
curl_setopt($_myRequest, CURLOPT_HTTPHEADER, Array("Content-Type: application/xml"));
curl_setopt($_myRequest, CURLOPT_POSTFIELDS,$xml );
curl_setopt($_myRequest, CURLOPT_RETURNTRANSFER, 1);
// do request, the response text is available in $_response
$_response = curl_exec($_myRequest);
$err = curl_error($_myRequest) ;
echo $err;
$_statusCode = curl_getinfo($_myRequest, CURLINFO_HTTP_CODE);
// close cURL resource, and free up system resources
curl_close($_myRequest);
return simplexml_load_string($_response);
}
I can't reproduce that problem with a simple example. Your example code seems to have a syntax error in CURLOPT_USERPWD, but I assume that is not related.
It sounds quite likely that there could there be rate limiting on the site you are querying, i.e. when you hit them a few times in a row they lock you out.
Perhaps you could debug by trying to run the query multiple times outside of the loop, i.e. calling it 1/2/3 times manually.

Google doesn't like my WAMP server. Anyone have any ideas?

Alright I'm at the end of the rope here. I've tried just about everything to try to get this working. I've done Google OAUTH API calls before on web hosts and everything works great, but when I use the same scripts on my WAMP server, google does not reply to my cURL posts. I've tested cURL post by posting to my own server as well as to this server someone set up:
http://www.newburghschools.org/testfolder/dump.php
And it works just fine. Here's the script I'm using after I get the user to allow my application:
<?
//This script handles the response from google. If the user as authed the app, it will continue the process by formulating another URL request to Google to get the users information. If there was an error, it will simply redirect to index.
include_once($_SERVER['DOCUMENT_ROOT']."/../src/php/google/initialize.php");
if (!isset($_GET['error'])) {
//No error found, formulate next HTTP post request to get our auth token
//Set URL and get our data encoded for POST
$url = "https://accounts.google.com/o/oauth2/token";
$fields = array(
'code' => $_GET['code'],
'client_id' => $google['clientID'],
'client_secret' => $google['clientSecret'],
'redirect_uri' => "http://www.dealertec.com/scripts/php/google/reg_response.php",
'grant_type' => "authorization_code"
);
//$data = http_build_query($fields);
$data = "code=".$fields['code'].'&client_id='.$fields['client_id'].'&client_secret='.$fields['client_secret'].'&redirect_uri='.$fields['redirect_uri'].'&grant_type='.$fields['grant_type'];
echo $data."<br> /";
//Begin cURL function
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
echo $response;
curl_close($ch);
//Decode the JSON and use it to make a request for user information
print_r($response);
$response = json_decode($response, true);
/*if (!isset($response['error'])) {
//No error in response, procede with final step of acquiring users information
$apiURL = "https://www.googleapis.com/oauth2/v1/userinfo?access_token=".$response['access_token'];
$apiCall = curl_init($apiURL);
curl_setopt($apiCall, CURLOPT_RETURNTRANSFER, true);
curl_setopt($apiCall, CURLOPT_HEADER, false);
$apiResponse = curl_exec($apiCall);
$apiResponse = json_decode($apiResponse, true);
var_dump($apiResponse);
}else{
echo $response['error'];
}*/
}else{
header("LOCATION: http://www.dealertec.com/");
}
?>
When I echo out what is supposed to be the google response, all I get is a singular "/". When I run this same exact script on my webhost, after changing back the DNS IP, it works fine. I'm thinking either Google doesn't like my WAMP server and won't even talk to it, or perhaps it's something with my cURL configuration. It's only a minor annoyance not being able to develop for Google API on my WAMP server, but if anyone has any ideas whatsoever, it would be greatly appreciated.
Thanks!
I was getting a FALSE coming back from curl_exec(), but this is how I fixed it:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
http://us3.php.net/manual/en/function.curl-setopt.php
hope this helps!

Why are the Twitter api calls so slow?

When I execute the following code it takes between 10-12 seconds to respond.
Is the problem with Twitter or with our server?
I really need to know as this is part of the code to display tweets on our website and a 12 second load time is just not acceptable!
function get_latest_tweets($username)
{
print "<font color=red>**". time()."**</font><br>";
$path = 'http://api.twitter.com/1/statuses/user_timeline/' . $username.'.json?include_rts=true&count=2';
$jason = file_get_contents($path);
print "<font color=red>**". time()."**</font><br>";
}
Thanks
When you put the URL into your browser (http://api.twitter.com/1/statuses/user_timeline/username.json?include_rts=true&count=2) how long does it take for the page to appear? If it's quick then you need to start the search at your server.
use curl instead of file_get_contents() to request, so that response will be compressed. Here is the curl function which iam using.
function curl_file_get_contents($url)
{
$curl = curl_init();
curl_setopt($curl,CURLOPT_URL,$url); //The URL to fetch. This can also be set when initializing a session with curl_init().
curl_setopt($curl,CURLOPT_RETURNTRANSFER,TRUE); //TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.
curl_setopt($curl,CURLOPT_ENCODING , "gzip");
curl_setopt($curl, CURLOPT_FAILONERROR, TRUE); //To fail silently if the HTTP code returned is greater than or equal to 400.
curl_setopt($curl,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
$contents = curl_exec($curl);
curl_close($curl);
return $contents;
}

building a 'simple' php url proxy

I need to implement a simple PHP proxy in a web application I am building (Its flash based and the destination service provider doesn't allow edits to their crossdomain.xml file)
Can any php gurus offer advice on the following 2 options? Also, I think, but am not sure, that I need to include some header info as well.
Thanks for any feedback!
option1
$url = $_GET['path'];
readfile($path);
option2
$content .= file_get_contents($_GET['path']);
if ($content !== false)
{
echo($content);
}
else
{
// there was an error
}
First of all, never ever ever include a file based only on user input. Imagine what would happen if someone would call your script like this:
http://example.com/proxy.php?path=/etc/passwd
Then onto the issue: what kind of data are you proxying? If any kind at all, then you need to detect the content type from the content, and pass it on so the receiving end knows what it's getting. I would suggest using something like HTTP_Request2 or something similar from Pear (see: http://pear.php.net/package/HTTP_Request2) if at all possible. If you have access to it, then you could do something like this:
// First validate that the request is to an actual web address
if(!preg_match("#^https?://#", $_GET['path']) {
header("HTTP/1.1 404 Not found");
echo "Content not found, bad URL!";
exit();
}
// Make the request
$req = new HTTP_Request2($_GET['path']);
$response = $req->send();
// Output the content-type header and use the content-type of the original file
header("Content-type: " . $response->getHeader("Content-type"));
// And provide the file body
echo $response->getBody();
Note that this code hasn't been tested, this is just to give you a starting point.
Here's another solution using curl
Can anyone comment??
$ch = curl_init();
$timeout = 30;
$userAgent = $_SERVER['HTTP_USER_AGENT'];
curl_setopt($ch, CURLOPT_URL, $_REQUEST['url']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
$response = curl_exec($ch);
if (curl_errno($ch)) {
echo curl_error($ch);
} else {
curl_close($ch);
echo $response;
}

Categories