Tag Archives: php

XMLRPC – Programmatically post on WordPress with an attach image to post in WordPress

This is how I make it!
Shared on StackOverflow
http://stackoverflow.com/questions/17722743/attach-image-to-post-in-wordpress-xmlrpc/24413781#24413781

References
http://codex.wordpress.org/XML-RPC_WordPress_API/Posts
http://codex.wordpress.org/Post_Status

require_once("IXR_Library.php.inc");
$title = 'My title';
$body = 'My body';
$category="category1, category2"; // Comma seperated pre existing categories. Ensure that these categories exists in your blog.
$keywords="keyword1, keyword2, keyword3";
$customfields=array('key'=>'Author-bio', 'value'=>'Autor Bio Here'); // Insert your custom values like this in Key, Value format

$title = htmlentities($title,ENT_NOQUOTES,@$encoding);
$keywords = htmlentities($keywords,ENT_NOQUOTES,@$encoding);

$content = array(
'post_title'=>$title,
'post_content'=>$body,
'post_type'=>'some_custom_post_type',
'post_status' => 'draft', // http://codex.wordpress.org/Post_Status
'mt_allow_comments'=>0, // 1 to allow comments
'mt_allow_pings'=>0, // 1 to allow trackbacks
'mt_keywords'=>$keywords,
'categories'=>array($category),
'custom_fields' => array($customfields)
);

// Create the client object
$client = new IXR_Client('http://example.com/xmlrpc.php');
$username = "wp_username";
$password = "wp_username_password";

$params = array(0,$username,$password,$content,true); // Last parameter is 'true' which means post immediately, to save as draft set it as 'false'

if (!$client->query('wp.newPost', $params)) {
die('Something went wrong - '.$client->getErrorCode().' : '.$client->getErrorMessage().'');

}
else
{
$post_id = $client->getResponse();
echo 'Inserted with id'.$post_id;
$picture = '/full/path/to/pic.jpg';
$content = array(
'name' => basename($picture),
'type' => mime_content_type($picture),
'bits' => new IXR_Base64(file_get_contents($picture)),
true
);
if (!$client->query('metaWeblog.newMediaObject', 1, $username, $password, $content)) {
die('
Something went wrong - newMediaObject'.$client->getErrorCode().' : '.$client->getErrorMessage().'
');
}
else
{
$media = $client->getResponse();
$content = array(
'post_status' => 'publish',
'post_thumbnail' => $media['id']
);
if (!$client->query('wp.editPost', 0, $username, $password, $post_id, $content)) {
die('
Something went wrong editPost - '.$client->getErrorCode().' : '.$client->getErrorMessage().'
');
}
}
}

Force download MP4 file – PHP or .htaccess

The PHP way – see below this one for a better way

In a client work, I need to force download a MP4 file on some special occasions.
PHP was the solution – in my case -.

rock ssd

This is the small script that I’v placed in download.php – the important stuff -.
Before I have a file_exist condition and other stuffs… but this is the RAW material!

 header ("Content-type: octet/stream");
 header ("Content-disposition: attachment; filename=".$filename.".mp4;");
 header("Content-Length: ".filesize($myFile));
 ob_clean();
 readfile($myFile);

Some of the MP4 files were beeing downloaded! Other weren’t. sh*t!
The problem was a PHP Fatal error!

[03-May-2014 08:09:18] PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 429363201 bytes) in /web/sites/user/domain.com/download.php on line 30

Well!, there are 3 ways to solve this.

  • Edit php.ini and increase the max memory size.
  • Add the following line to the PHP file…
ini_set('memory_limit','128M');
  • Or add the following line to .htaccess
php_value memory_limit 128M

First solution is the best option, since the other 2 might not work due to security settings.

The PHP way *ultimate solution*

This solution we don’t need to increase PHP memory limit.
This will read 8kb of file and push it to the client AND not the full file!

header ("Content-type: octet/stream");
header ("Content-disposition: attachment; filename=".$filename.".mp4;");
header("Content-Length: ".filesize($myFile));
ob_clean();
$handle=fopen($_REQUEST['file'], 'rb');
while (!feof($handle))
{ 
    echo fread($handle, 8192);
    flush();
}
fclose($handle);

The .htaccess way

The normal file type for MP4 is video/mp4.
This way the browser will try to play it and won’t download it.

We can force .htaccess to change it and force the download.

Lets say that we want to force the download when the URL has a ?dl.

RewriteCond %{QUERY_STRING} dl
RewriteRule .*\.mp4 - [T=application/octet-stream]

This way .htaccess www.domain.com/video.mp4?dl and change the file type to application/octet-stream – the one that forces the download.

 

Top 5 security flaws within websites and how to prevent them

So we’re now into 2014 and websites are starting to become way more advanced by implementing new technologies such as AJAX, fancy web forms and even web sockets. However, little do webmaster’s realise that they’re opening a space for huge security flaws within their websites / web applications / web services for malicious hackers to take advantage of.

Read more at http://joel-murphy.com/top-5-security-flaws-within-websites-and-how-to-prevent-them/

 

Add or edit a post to wordpress with PHP

Since 2010 that I’m using this script to post content on some of my 10s blogs.
I got it from WORDPRESS XMLRPC – POSTING CONTENT FROM OUTSIDE WORDPRESS ADMIN.

Add a post to wordpress with PHP

<?php

require_once("IXR_Library.php.inc");
$client->debug = true; //Set it to false in Production Environment

$title="Blog Title"; // $title variable will insert your blog title
$body="Blog Content"; // $body will insert your blog content (article content)
$category="category1, category2"; // Comma seperated pre existing categories. Ensure that these categories exists in your blog.
$keywords="keyword1, keyword2, keyword3";
$customfields=array('key'=>'Author-bio', 'value'=>'Autor Bio Here'); // Insert your custom values like this in Key, Value format

$title = htmlentities($title,ENT_NOQUOTES,$encoding);
$keywords = htmlentities($keywords,ENT_NOQUOTES,$encoding);

$content = array(
'title'=>$title,
'description'=>$body,
'mt_allow_comments'=>0, // 1 to allow comments
'mt_allow_pings'=>0, // 1 to allow trackbacks
'post_type'=>'post',
'mt_keywords'=>$keywords,
'categories'=>array($category),
'custom_fields' => array($customfields)
);

// Create the client object
$client = new IXR_Client('Your Blog Path/xmlrpc.php');
$username = "USERNAME";
$password = "PASSWORD";

$params = array(0,$username,$password,$content,true); // Last parameter is 'true' which means post immediately, to save as draft set it as 'false'

// Run a query for PHP if (!$client->query('metaWeblog.newPost', $params)) {
die('Something went wrong - '.$client->getErrorCode().' : '.$client->getErrorMessage());
} else { echo "Article Posted Successfully"; } ?>

Edit a post in wordpress with PHP

Continue reading Add or edit a post to wordpress with PHP

HipHop-Vagrant-VM

HipHop for PHP (shortened as HipHop) describes a series of PHP execution engines and improvements created by Facebook. The original motivation of HipHop was to save resources on Facebook servers, given the large PHP codebase of facebook.com. As development of HipHop progressed, it was realized that HipHop could substantially increase the speed of PHP applications in general. Increases in web page generation throughput by factors of up to 6 have been observed over Zend PHP.

Source: http://en.wikipedia.org/wiki/HipHop_for_PHP

 

Setup a test environment for HipHop VM.
The final VM will contain HHVM, Nginx, PHP, MySQL, ab.

Some graphs comparing PHP 5.3, PHP 5.5 and HHVM.
https://blog.liip.ch/archive/2013/10/29/hhvm-and-symfony2.html