What is going wrong with this API? - php

I am coding a website for my friend whom plays Habbo Hotel which is a virtual game. He linked me to some API.
http://habboemotion.com/guide/habinfo & http://habboemotion.com/guide/habboapi
I have been using this code to show the data from the api.
<?php
$info = habbo( "Tyler", "com" );
if( $info ) {
foreach( $info->user AS $name ) {
echo $name->motto;
}
} else {
echo "Habbo not found";
}
?>
Why is nothing appearing? It just appears to be a blank screen.

As the previous person said, make sure to include your habbo() function.
I changed the habbo() function to remove gzip compression and gzinflate(). That seemed to fix the blank page issue. However it seems to take a few seconds to load the page and is on the slow side.
It would also appear that $user->motto isn't allowed. As such, I've replaced it with $friends->motto.
Hope this helps! I am still very new to APIs.
<?php
error_reporting(E_ALL); // Debugging
ini_set('display_errors', 1); // Debugging
function habbo( $name, $hotel ) {
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, "https://www.habbo." . $hotel . "/api/public/users" );
curl_setopt( $ch, CURLOPT_HEADER, false );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Accept-Encoding: identity' ) ); // Changed to "identity"
curl_setopt( $ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT'] );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0 );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0 );
$response = curl_exec( $ch ); // Added
//$get = gzinflate( substr( curl_exec( $ch ), 10, -8 ) );
//preg_match( "/setCookie\((.*)\);/", $get, $get );
//$get = explode( ",", str_replace( array( "'", " " ), "", $get[1] ) );
//curl_setopt( $ch, CURLOPT_HTTPHEADER, array( "Cookie:" . $get[0] . "=" . $get[1] ) );
curl_setopt( $ch, CURLOPT_URL, "http://www.habbo." . $hotel . "/api/public/users?name=" . $name );
$id = json_decode( curl_exec( $ch ) );
if( isset( $id ) && $id->profileVisible == 1 ) {
curl_setopt( $ch, CURLOPT_URL, "http://www.habbo." . $hotel . "/api/public/users/" . $id->uniqueId . "/profile" );
$info = json_decode( curl_exec( $ch ) );
} else
$info = false;
curl_close( $ch );
return $info;
}
Here is the function call:
$info = habbo( "Tyler", "com" );
if( $info ) {
foreach( $info->friends AS $friend ) {
echo $friend->motto . "<br />";
}
} else {
echo "habbo not found or homepage hidden";
}
?>

Related

Need help on Facebook API how to get data from array with multiple fields

$endpointFormat = ENDPOINT_BASE . '{page-id}?fields=about,bio,description,new_like_count,talking_about_count,category,fan_count,link,name,rating_count,website,whatsapp_number,followers_count,country_page_likes,were_here_count,location&access_token={access-token}';
$statsAccountEndpoint = ENDPOINT_BASE . $pageId;
$statParams = array(
array ('fields' => 'about','bio','description','new_like_count', 'talking_about_count','category', 'fan_count', 'link','name','rating_count','website','whatsapp_number','followers_count','country_page_likes','were_here_count','location'),
'access_token' => $singleRow['fbaccesstoken']
);
// add params to endpoint
$statsAccountEndpoint .= '?' . http_build_query( $statParams );
// setup curl
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $statsAccountEndpoint );
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, false );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
// make call and get response
$response = curl_exec( $ch );
curl_close( $ch );
$responseArray = json_decode( $response, true );
Printing out the statParams
Printing out responseArray which only shows the name and page id
Just wanting to get the stats of the page im linking, tried with only 1 field variable before and it showed fine. Thanks for your time!
$statParams = array(
'fields' => 'about,bio,description,new_like_count,talking_about_count,category,fan_count,link,name,rating_count,website,whatsapp_number,followers_count,country_page_likes,were_here_count,location',
);
just had to make it so that everything is in one big '' instead of separate.

MediaWiki API - Using new line for bullet points

I'm having trouble trying to do bullet points using the MediaWiki API, I can't work out how to get it to do the new line, everything ends up on the same line.
I've tried using
$maps[] = "* " . $mapName . "<br/>";
$maps[] = "* " . $mapName . "\n";
$maps[] = "* " . $mapName . "\r\n";
None of which work
I'm updating the wiki using
$endPoint = "URL";
$params4 = [
"action" => "edit",
"title" => $title,
"text" => $content,
"token" => $csrf_Token,
"format" => "json"
];
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $endPoint );
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query( $params4 ) );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_COOKIEJAR, "cookie.txt" );
curl_setopt( $ch, CURLOPT_COOKIEFILE, "cookie.txt" );
$output = curl_exec( $ch );
curl_close( $ch );
Got it working using
$maps[] = "* [[" . $mapName . "]]<br/>%0D%0A";

POST request returns empty when called by 3rd app but not when called manually

I have an app in adalo.com which calls my custom api (simple PHP file) api.domain.com. When it is called, there are two requests to jotform.com.
First is GET and works nicely. And second is POST.
When I tried the POST request on localhost with custom data, it works. It also work when I try POST request via https://reqbin.com/.
So the problem is, as it seems, when Adalo app calls my API after I submit form.
Everything works just not the POST request (e.g. I got all data from Adalo form in the log file and GET request to Jotform works).
Below the code of function.
function post_data( $path, $post_data = array() ) {
if( empty( $path ) || empty( $post_data ) ) return false;
$api_url = JOTFORM_API_URL;
$api_key = JOTFORM_API_KEY;
$post_data_url = implode( '&', $post_data );
// error_log( 'post_data' );
// error_log( $post_data_url );
$url = "{$api_url}{$path}?apikey={$api_key}&{$post_data_url}";
// error_log( 'url' );
// error_log( $url );
// echo $url . "\n\n";
// return;
$curl = curl_init();
curl_setopt( $curl, CURLOPT_URL, $url );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $curl, CURLOPT_USERAGENT, 'JOTFORM_PHP_WRAPPER');
curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt( $curl, CURLOPT_POST, true);
$exec = curl_exec( $curl );
if ( $exec === false ) {
error_log( curl_error( $curl ) );
}
$data = json_decode( $exec, true );
curl_close( $curl );
error_log( var_dump( $data ) );
return $data;
}

eBay api search item

I want to get items by keyword search.
<?php
error_reporting(E_ALL);
require_once("includes/config.php");
// Construct the HTTP GET call
$apicall = "https://api.ebay.com/buy/browse/v1/item_summary/search?"; //
URL to call
$apicall .= "q=GTR"; //search keyword
$apicall .= "limit=3";
// Create headers to send with CURL request.
$headers = array
(
'X-EBAY-API-APP-ID: ' . $sapp_id,
'X-EBAY-API-DEV-NAME: ' . $dev_id,
'X-EBAY-API-CERT-NAME: ' . $cert_id,
'X-EBAY-C-ENDUSERCTX: 5337984774'
);
// Send request to eBay and load response in $response
$connection = curl_init();
curl_setopt($connection, CURLOPT_URL,
"https://api.ebay.com/buy/browse/v1/item_summary/search?");
curl_setopt($connection, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($connection, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($connection, CURLOPT_HTTPHEADER, $headers);
curl_setopt($connection, CURLOPT_POST, 1);
curl_setopt($connection, CURLOPT_POSTFIELDS, $apicall);
curl_setopt($connection, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($connection);
curl_close($connection);
// Load the call and capture the document returned by eBay API
$resp = $response;
// Output response
echo("<pre>");
print_r($resp);
//To retrieve specific values of the array in PHP, you will need to use
object operator
// $value = $resp->Item->ConvertedCurrentPrice;
// $value = $resp->Item->ConvertedCurrentPrice;
// print $xml->Product->Title;
echo("</pre>");
?>
i got this error when i run.
{
"errors": [
{
"errorId": 3001,
"domain": "ROUTING",
"category": "REQUEST",
"message": "Request Rejected",
"longMessage": "The Request has errors. For help, see the documentation for this API."
}
]
}
Since i am beginner i don't know what to do.
i tried xml request method and it's work fine but i want to do it with REST API method.
i follow this docs - Official eBay search api documentation
i like to know why i am getting this error and what's the correct way to achieve this search query ?
Not tested as I don't have an eBay developer account but perhaps the following might help. The code below will issue a GET request with the querystring as part of the url as the documentation suggests
<?php
error_reporting( E_ALL );
require_once("includes/config.php");
/*
you can download from https://curl.haxx.se/docs/caextract.html
*/
$cacert='c:/wwwroot/cacert.pem'; # edit as appropriate to point to valid `cacert.pem` file
$verbose=true;
$endpoint = "https://api.ebay.com/buy/browse/v1/item_summary/search?";
$args=array(
'q' => 'GTR',
'limit' => 3
);
$url=sprintf( '%s%s', $endpoint, http_build_query( $args ) );
/*
for advanced debug information we create a stream
and output to that during the request. At the end
of the request the stream is processed and we can
view that data.
*/
if( $verbose )$vbh = fopen('php://temp', 'w+');
$headers = array(
'X-EBAY-API-APP-ID: ' . $sapp_id,
'X-EBAY-API-DEV-NAME: ' . $dev_id,
'X-EBAY-API-CERT-NAME: ' . $cert_id,
'X-EBAY-C-ENDUSERCTX: 5337984774'
);
$curl = curl_init();
curl_setopt( $curl, CURLOPT_URL, $url );
curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, true );
curl_setopt( $curl, CURLOPT_CAINFO, $cacert );
curl_setopt( $curl, CURLOPT_SSL_VERIFYHOST, 2 );
curl_setopt( $curl, CURLOPT_HTTPHEADER, $headers );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $curl, CURLOPT_USERAGENT, 'curl' );
if( $verbose ){
curl_setopt( $curl, CURLOPT_VERBOSE, true );
curl_setopt( $curl, CURLOPT_NOPROGRESS, true );
curl_setopt( $curl, CURLOPT_STDERR, $vbh ); # the stream object
}
/* make the request and get info */
$response = curl_exec( $curl );
$info=(object)curl_getinfo( $curl );
$errors=curl_error( $curl );
/* store the verbose debug info */
if( $verbose ){
rewind( $vbh );
$debug=stream_get_contents( $vbh );
fclose( $vbh );
}
curl_close( $curl );
if( $info->http_code==200 ){
printf( '<pre>%s</pre>', print_r( $response, 1 ) );
} else{
printf( '<pre>%s</pre>', print_r( $verbose ? $debug : $info ,1 ) );
printf( '<pre>%s</pre>', print_r( $errors, 1 ) );
}
?>

how to know if a certain resource exist?

For instance, I want to know if these files exists.
http://www.stackoverflow.com/favicon.ico
http://www.stackoverflow.com/reset.css
and then download it ( if exixts obviously ).
<?php
if( ( $file = file_get_contents( 'http://www.stackoverflow.com/favicon.ico' ) ) ) {
echo "file exists.";
file_put_contents( 'favicon.ico', $file );
}
else {
echo "File does not exist.";
}
Try this:
<?php
$ch = curl_init();
$url = 'YOUR_URL_HERE'; // the url you want to check
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 20 );
curl_setopt( $ch, CURLOPT_USERAGENT, $_SERVER[ 'HTTP_USER_AGENT' ] );
// make "HEAD" request
curl_setopt( $ch, CURLOPT_HEADER, true );
curl_setopt( $ch, CURLOPT_NOBODY, true );
$res = curl_exec( $ch );
$res = explode( ' ', substr( $res, 0, strpos( $res, "\n" ) ) );
// if 404, file does not exist
if( $res[ 1 ] != 404 ) {
$file = file_get_contents( $url );
} else {
// This url does not exist
$file = '';
}
curl_close( $ch );
?>
Hope this helps.

Categories