Using number_format to add thousand separator - php

I'm trying to quite simply take the number that is inside the variable $output and turn it into a number with a thousand separator. The number it current outputs is 49995 but I want it to appear as 49,995.
Having some trouble. Help?
function twitter_followers($user = 'mytwitterusername'){
// Build Twitter api url
$apiurl = "http://api.twitter.com/1/users/show.json?screen_name={$user}";
//cache request
$transient_key = $user . "_twitter_followers";
// If cached (transient) data are used, output an HTML
// comment indicating such
$cached = get_transient( $transient_key );
if ( false !== $cached ) {
return $cached;
}
// Request the API data, using the constructed URL
$remote = wp_remote_get( esc_url( $apiurl ) );
// If the API data request results in an error, return
// an appropriate comment
if ( is_wp_error( $remote ) ) {
return '<p>Twitter unaviable</p>';
}
// If the API returns a server error in response, output
// an error message indicating the server response.
if ( '200' != $remote['response']['code'] ) {
return '<p>Twitter responded with an HTTP status code of '. esc_html( $remote['response']['code']) . '</p>';
}
// If the API returns a valid response, the data will be
// json-encoded; so decode it.
$data = json_decode( $remote['body'] );
$output = $data->followers_count;
$followers = number_format($output,2,'.',',');
set_transient( $transient_key, $output, 600 );
return $followers;
}

I've tested the following code and it works:
$output = 49995;
$followers = number_format( $output , 0 , '.' , ',' );
echo $followers;
Not sure why your code is not working. Also make sure to set the second parameter to 0, unless you want decimal points. Perhaps the value of $output initially is a string and you need to cast it as an integer before putting it through number_format()?

Your number_format seems to be right. Try an
$output = intval($data->followers_count);
before calling it, maybe there is an issue after decoding the value.

Related

Change the number format displayed in echo statement in php mysql [duplicate]

I'm trying to quite simply take the number that is inside the variable $output and turn it into a number with a thousand separator. The number it current outputs is 49995 but I want it to appear as 49,995.
Having some trouble. Help?
function twitter_followers($user = 'mytwitterusername'){
// Build Twitter api url
$apiurl = "http://api.twitter.com/1/users/show.json?screen_name={$user}";
//cache request
$transient_key = $user . "_twitter_followers";
// If cached (transient) data are used, output an HTML
// comment indicating such
$cached = get_transient( $transient_key );
if ( false !== $cached ) {
return $cached;
}
// Request the API data, using the constructed URL
$remote = wp_remote_get( esc_url( $apiurl ) );
// If the API data request results in an error, return
// an appropriate comment
if ( is_wp_error( $remote ) ) {
return '<p>Twitter unaviable</p>';
}
// If the API returns a server error in response, output
// an error message indicating the server response.
if ( '200' != $remote['response']['code'] ) {
return '<p>Twitter responded with an HTTP status code of '. esc_html( $remote['response']['code']) . '</p>';
}
// If the API returns a valid response, the data will be
// json-encoded; so decode it.
$data = json_decode( $remote['body'] );
$output = $data->followers_count;
$followers = number_format($output,2,'.',',');
set_transient( $transient_key, $output, 600 );
return $followers;
}
I've tested the following code and it works:
$output = 49995;
$followers = number_format( $output , 0 , '.' , ',' );
echo $followers;
Not sure why your code is not working. Also make sure to set the second parameter to 0, unless you want decimal points. Perhaps the value of $output initially is a string and you need to cast it as an integer before putting it through number_format()?
Your number_format seems to be right. Try an
$output = intval($data->followers_count);
before calling it, maybe there is an issue after decoding the value.

Check if PDF File is available before downloading?

We are using Woocommerce for our ecom website and in order to automatically generate government-approved invoices for customers we use a certified online invoicing software.
I am making an API request to this invoicing software in order to retrieve the generated invoice document from their database, this is the code:
// On Order complete > Get document ID from order > access Moloni invoicing API > get document link GETPDFLINK > Sanitize the string and get Hash > generate the final document link > access it and download the PDF
function download_moloni_document_id( $order_id, $order ) {
// Retreive from the Database table moloni_api the access token from column main_token
global $wpdb;
$table_name = "db_invoicing_api";
$retrieve_data = $wpdb->get_results( "SELECT * FROM $table_name WHERE id = 1" );
foreach ($retrieve_data as $retrieved_data) {
$maintoken = $retrieved_data->main_token;
}
// Get document ID from the order
$documentid = get_post_meta($order->id, '_moloni_sent', true);
// Connect to moloni API and getpdflink
$url = "https://api.moloni.pt/v1/documents/getPDFLink/?access_token=$maintoken";
$postData = array(
'company_id' => '12345',
'document_id' => $documentid );
$arguments = array(
'method' => 'POST',
'headers' => array(
'Content-type: application/x-www-form-urlencoded'
),
'body' => $postData,
);
$response = wp_remote_post( $url, $arguments );
if ( is_wp_error( $response ) ) {
$error_message = $response->get_error_message();
return "Something went wrong: $error_message";
} else {
echo '<pre>';
var_dump( wp_remote_retrieve_body( $response ) );
echo '</pre>';
// jsondecode the string received by the API to remove backslashes, get and parse the URL and extract the HASH key
$response2 = wp_remote_retrieve_body($response);
parse_str(parse_url(json_decode($response2, true)['url'], PHP_URL_QUERY), $result);
$hash = $result['h'];
// Assemble the Invoice HTML download URL with the Hash and document ID
$fileUrlpdf = "https://www.moloni.pt/downloads/index.php?action=getDownload&h=$hash&d=$documentid&e=$loginmail&i=1&t=n";
$pdforderid = $order->id;
// Save the file with document id name to server location
$saveTopdf = "ftp://myserver/INVOICES/evo-$pdforderid.PDF";
file_put_contents(
$saveTopdf,
file_get_contents($fileUrlpdf)
);
} }
add_action( 'woocommerce_order_status_completed', 'download_moloni_document_id', 20, 2 );
In the end you can see I use
file_put_contents(
$saveTopdf,
file_get_contents($fileUrlpdf) );
in order to visit the link, retrieve the PDF and download it.
This works well then the link is generated successfully and sends to a direct download of the invoice PDF
The problem I have is is, sometimes there is an issue and the invoice PDF is not generated, this makes it so that the final link $fileUrlpdf does not lead to a download but instead to a web page with an error message saying something like "No Documents Found" which leads this code to download a PDF of dozens of pages containing the source code/HTML of that page. This is a problem because the PDFs (invoices) are later automatically printed by our system, so we sometimes end up with hundreds of pages of HTML code instead of the invoices.
I have tried to solve this in the following way through conditions:
to check if the download/PDF file exists
if (file_exists($fileUrlpdf)) {
file_put_contents(
$saveTopdf,
file_get_contents($fileUrlpdf)); }
one that would check the $response array for the error message and not proceed (because
$invalid = 'No Documents Found'; if (strpos($response, $valid) !== false) {echo 'No Documents found'; else { *download the PDF* } }
I have also considered the possibility of
checking if the page $fileUrlpdf would contain "No Documents Found", to not download it but I haven't been able to figure this one out either.
Bear with me as you can see my experience with PHP is limited so I would like to ask, what would be the best practice here? What approach would you suggest?
Thank you very much in advance for the attention and advice.
Either look at using the API to fetch the invoice/document and handle errors that way moloni.pt/dev
Or you could look at checking the Content-Type of the response headers as shown below
get_headers
$fileUrlpdf = "https://www.moloni.pt/downloads/index.php?action=getDownload&h=$hash&d=$documentid&e=$loginmail&i=1&t=n";
$fileHeaders = get_headers($fileUrlpdf, true)
if($fileHeaders['Content-Type'] === 'application/pdf') {
// PDF response
}

Convert php integer to string to array? [duplicate]

I'm trying to quite simply take the number that is inside the variable $output and turn it into a number with a thousand separator. The number it current outputs is 49995 but I want it to appear as 49,995.
Having some trouble. Help?
function twitter_followers($user = 'mytwitterusername'){
// Build Twitter api url
$apiurl = "http://api.twitter.com/1/users/show.json?screen_name={$user}";
//cache request
$transient_key = $user . "_twitter_followers";
// If cached (transient) data are used, output an HTML
// comment indicating such
$cached = get_transient( $transient_key );
if ( false !== $cached ) {
return $cached;
}
// Request the API data, using the constructed URL
$remote = wp_remote_get( esc_url( $apiurl ) );
// If the API data request results in an error, return
// an appropriate comment
if ( is_wp_error( $remote ) ) {
return '<p>Twitter unaviable</p>';
}
// If the API returns a server error in response, output
// an error message indicating the server response.
if ( '200' != $remote['response']['code'] ) {
return '<p>Twitter responded with an HTTP status code of '. esc_html( $remote['response']['code']) . '</p>';
}
// If the API returns a valid response, the data will be
// json-encoded; so decode it.
$data = json_decode( $remote['body'] );
$output = $data->followers_count;
$followers = number_format($output,2,'.',',');
set_transient( $transient_key, $output, 600 );
return $followers;
}
I've tested the following code and it works:
$output = 49995;
$followers = number_format( $output , 0 , '.' , ',' );
echo $followers;
Not sure why your code is not working. Also make sure to set the second parameter to 0, unless you want decimal points. Perhaps the value of $output initially is a string and you need to cast it as an integer before putting it through number_format()?
Your number_format seems to be right. Try an
$output = intval($data->followers_count);
before calling it, maybe there is an issue after decoding the value.

How to create a facebook feed for wordpress?

I'm new to worpdress and completed a basic development in wordpress course, our final project it's to bring a facebook page data like states and pics, to be displayed in a word press site, to be specific to be listed in a page, I've been researching using facebook developers and found out, tha when querying this url, https://www.facebook.com/feeds/page.php?id=[pageID]&format=json y got the JSON with all de data, also I've tested in http://jsonviewer.net/ and looks good, no I'm stuck in how to make that JSON to be displayed on a page at my site.
Please need some help with this,
You can use a Shortcode for that [fb-page id="ID-NUM"], use the function wp_remote_get() to pull the feed and then convert the returned JSON into array using PHP's json_decode().
add_shortcode( 'fb-page', 'shortcode_so_25919996' );
function shortcode_so_25919996( $atts )
{
if( empty( $atts['id'] ) )
return 'Please, provide an ID';
# Request URL content.
$url = 'https://www.facebook.com/feeds/page.php?id=' . $atts['id'] . '&format=json';
$response = wp_remote_get( $url );
if ( is_wp_error( $response ) )
return 'Error fetching the feed.';
# Response OK. Decode the response body.
$json_to_array = json_decode( wp_remote_retrieve_body( $response ), true );
# Print the array as code block. Use a loop to build the output as HTML string.
return '<pre><code>' . print_r( $json_to_array, true ) . '</code></pre>';
}
Or you can call this same function as:
<?php echo shortcode_so_25919996( array( 'id' => 'ID-NUM' ) ); ?>

Download rapidshare file using rapidshare api in php

I am trying to download a rapidshare file using its "download" subroutine as a free user. The following is the code that I use to get response from the subroutine.
function rs_download($params)
{
$url = "http://api.rapidshare.com/cgi-bin/rsapi.cgi?sub=download&fileid=".$params['fileid']."&filename=".$params['filename'];
$reply = #file_get_contents($url);
if(!$reply)
{
return false;
}
$result_arr = array();
$result_keys = array(0=> 'hostname', 1=>'dlauth', 2=>'countdown_time', 3=>'md5hex');
if( preg_match("/DL:(.*)/", $reply, $reply_matches) )
{
$reply_altered = $reply_matches[1];
}
else
{
return false;
}
foreach( explode(',', $reply_altered) as $index => $value )
{
$result_arr[ $result_keys[$index] ] = $value;
}
return $result_arr;
}
For instance; trying to download this...
http://rapidshare.com/files/440817141/AutoRun__live-down.com_Champ.rar
I pass the fileid(440817141) and filename(AutoRun__live-down.com_Champ.rar) to rs_download(...) and I get a response just as rapidshare's api doc says.
The rapidshare api doc (see "sub=download") says call the server hostname with the download authentication string but I couldn't figure out what form the url should take.
Any suggestions?, I tried
$download_url = "http://$the-hostname/$the-dlauth-string/files/$fileid/$filename"
and a couple other variations of the above, nothing worked.
I use curl to download the file, like the following;
$cr = curl_init();
$fp = fopen ("d:/downloaded_files/file1.rar", "w");
// set curl options
$curl_options = array(
CURLOPT_URL => $download_url
,CURLOPT_FILE => $fp
,CURLOPT_HEADER => false
,CURLOPT_CONNECTTIMEOUT => 0
,CURLOPT_FOLLOWLOCATION => true
);
curl_setopt_array($cr, $curl_options);
curl_exec($cr);
curl_close($cr);
fclose($fp);
The above curl code doesn't seem to work, nothing gets downloaded. Probably its the download url that is incorrect.
Also tried this format for the download url:
"http://rs$serverid$shorthost.rapidshare.com/files/$fileid/$filename"
With this curl writes a file entry but that is all it does(writes a 0/1 kb file).
Here is the code that I use to get the serverid, shorthost, among a few other values from rapidshare.
function rs_checkfile($params)
{
$url = "http://api.rapidshare.com/cgi-bin/rsapi.cgi?sub=checkfiles_v1&files=".$params['fileids']."&filenames=".$params['filenames'];
// the response from rapishare would a string something like:
// 440817141,AutoRun__live-down.com_Champ.rar,47768,20,1,l3,0
$reply = #file_get_contents($url);
if(!$reply)
{
return false;
}
$result_arr = array();
$result_keys = array(0=> 'file_id', 1=>'file_name', 2=>'file_size', 3=>'server_id', 4=>'file_status', 5=>'short_host'
, 6=>'md5');
foreach( explode(',', $reply) as $index => $value )
{
$result_arr[ $result_keys[$index] ] = $value;
}
return $result_arr;
}
rs_checkfile(...) takes comma seperated fileids and filenames(no commas if calling for a single file)
Thanks in advance for any suggestions.
You start by requesting ?sub=download&fileid=X&filename=Y, and it returns $hostname,$dlauth,$countdown,$md5hex.. since you're a free user you have to delay for $countdown seconds, and then call ?sub=download&fileid=X&filename=Y&dlauth=Z to perform the download.
There's a working implementation in python here that would probably answer any of your other questions.

Categories