Consistent error accessing Soundcloud API with PHP on bluehost server - php

I'm having trouble with Soundcloud.php on my server. Although it runs just fine on my MAMP installation. Basically my test page won't load and an error is logged declaring a problem in Soundcloud.php:
[03-Apr-2012 03:50:57] PHP Parse error: syntax error, unexpected T_FUNCTION, expecting ')' in /home2/mysite/public_html/mysubdomain/Soundcloud.php on line 685
the test code is fine - here it is for reference:
<?php
require 'Soundcloud.php';
$soundcloud = new Services_Soundcloud('Client_ID','Client_Secret', 'Redirect_URI');
try {
$info = json_decode($soundcloud->get('tracks', array('user_id' => 'blumarten')), true);
print_r($info);
}
catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) {
exit($e->getMessage());
}
I just had the account upgraded to PHP 5.3 but the error still occurs, any ideas?

SoundCloud PHP API and documentation are very poor and buggy at the moment , hopefully will get better.
Replace this code around line 720
$postData = array_map(function ($track) {
return 'playlist[tracks][][id]=' . $track;
}, $trackIds);
With this:
$postData = array_map("suckySc", $trackIds);
And add this function to the top of the file, before the class opening:
function suckySc ($track){
return 'playlist[tracks][][id]=' . $track;
}

Related

PHP SoapClient, I just cannot get it to work

I am trying to learn how to access soap web services via PHP. I can get a list of functions available. I cannot get a return from a SoapClient function. My code is as follows:
<?php
date_default_timezone_set('America/Chicago');
$fcs = 'fcs is initialized';
$url = 'url is initialized';
$res = 'res is initialized';
$url = 'http://wsf.cdyne.com/WeatherWS/Weather.asmx?WSDL';
$param = array('ZIP' => '72685');
try {
$client = new SoapClient($url);
$fcs = $client->__getFunctions();
$res = $client->GetCityForecastByZIP($param);
} catch (Exception $e) {
echo "<h2>Exception Error!</h2>";
echo $e->getMessage();
}
echo '<br> url = '.$url;
echo '<br> fcs = '.$fcs;
echo '<br> res = '.$res.'<br>';
?>
I have tried about 6 Soap testing URLs that google can find. Some (http://www.webservicex.com/globalweather.asmx?wsdl) of them have evolved into something else. The one I tried the most (http://wsf.cdyne.com/WeatherWS/Weather.asmx?WSDL) did seem to be having the same trouble I was in it's web page implementation, http://wsf.cdyne.com/WeatherWS/Weather.asmx.
the Exception Error! is
"Server was unable to process request. ---> A network-related or
instance-specific error occurred while establishing a connection to
SQL Server."
To repeat my results,
1) the Soap client object creation seems to work w/o error.
2) __getFunctions seems to work w/o error.
3) trying to get a result from any of the functions produces the error shown above.
Questions:
1) Is there any error in my code that would cause it not to work?
2) What is a good working Soap Web Service URL sandbox?
It looks like its the weather service. To rule out your code go to http://wsf.cdyne.com/WeatherWS/Weather.asmx?op=GetCityWeatherByZIP
Add your ZIP into the Zip box and hit 'Invoke' and you will get an SQL error. So I would say its safe to assume its not your code and its the server.

PHP OAuth errors handling

I am using an OAuth library to connect to Etsy shop API. Code looks like this:
foreach ($transactions as $transactionSingle) {
try {
$oauth = new OAuth('xxx', 'xxx',
OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_URI);
$oauth->setToken($access_token, $access_token_secret);
$data = $oauth->fetch("https://openapi.etsy.com/v2/users/".$transactionSingle['buyer_id']."/profile", null, OAUTH_HTTP_METHOD_GET);
$json = $oauth->getLastResponse();
$results = json_decode($json, true);
} catch (OAuthException $e) {
return null;
}
}
Now the problem is that I run this code multiple times on foreach and if this URL is wrong and it fails to get any data - the whole function stops and doesn't continue anymore. It works perfectly until an old user ID is passed to URL and $oauth->fetch just reuturns message :
Invalid auth/bad request (got a 404, expected HTTP/1.1 20X or a
redirect)
Any ideas how to continue to run the function despite any errors?
The problem was in error cathing itself. Needed backslash on
catch (\OAuthException $e) {
}
Now the code catches the errors and continues if no code is provided inside catch.

syntax error, unexpected T_OBJECT_OPERATOR? Facebook

I am practicing with facebook PHP api
when i am trying to make a login function
here comes out this warnning
syntax error, unexpected T_OBJECT_OPERATOR in /home/u528851895/public_html/Desktap/facebook-php-sdk-v4-4.0-dev/src/Facebook/FacebookResponse.php on line 137
but it's ok when i use mamp localhost
here's line 136~138 in FacebookResponse.php:
public function getGraphObject($type = 'Facebook\GraphObject') {
return (new GraphObject($this->responseData))->cast($type);
}
Upgrade your php version or do something like this
public function getGraphObject($type = 'Facebook\GraphObject') {
$obj = new GraphObject($this->responseData);
return $obj->cast($type);
}

Unexpected T_FUNCTION, but where?

I've gotten my site to run perfectly on my local machine. However, migrating to production, I get roadblocked by "Unexpected T_FUNCTION ..." error.
I've stripped everything prior to the following code and still get the error reported for line 3. I just can't figure this out.
<?php
// Auth Check.
$authCheck = function() use ($app) { <---- Line 3
You are getting :
Parse error: syntax error, unexpected T_FUNCTION in [...][...] on line 4
becasue you are using PHP version less than PHP 5.3.0 upgrade your php or try using globals
PHP 5.3.0+
$app = "Hello Wolrd";
$authCheck = function () use($app) {
echo $app;
};
$authCheck();
Below PHP 5.3.0
$app = "Hello Wolrd";
$authCheck = function () {
global $app;
echo $app;
};
$authCheck();
Both would output
Hello Wolrd

Why am I getting errors thrown on PHP code wrapped in a try-catch?

<?php
try {
$attrs = $xml->attributes();
$code = $attrs['_Code'];
}
catch (Exception $e)
{
$code = '';
}
?>
Gets me:
Fatal error: Call to a member function attributes() on a non-object on
line 6
Why am I getting errors thrown on code wrapped in a try-catch??
NOTE: It is possible to avoid this error by using the following code. (The question is not about avoiding the error, but why it's not being caught- still I thought I'd share the non-erroring code anyway)
if (is_object($xml) && method_exists($xml,'attributes')) {
$attrs = $xml->attributes();
$code = !empty($attrs['_Code'])?$attrs['_Code']:'';
}
else {
$code = '';
}
PHP fatal errors cannot be caught. I don't know the specifics of what you're doing, but you'll have to figure out some other way to test whether $xml->attributes() will work or not.
Also, swallowing every error and not logging it anywhere is bad practice because when stuff starts breaking you won't have any idea of why.
try/catch only works for exceptions, not parse errors. You have to test to make sure that $xml has an attributes method to avoid such an error (could be called a null pointer, but not exactly).

Categories