upload a image with post method with xml - iphone

I am bit stuck about how to pase the image in post method with xml parsing .
I am able to send the string values but dont know how to send the images .
A code snippet will be very helpful or any tutorial or link.
My url is something like this
http://66.45.25.240/XML/EditProfile.aspx?myguid=45&guid=45&name=aditya&email=xyz#gmail.com&cell=1234567890&title=Title&twitterhandle=tutu.com&facebookhandle=ad#ad.com&picture=a124.jpg
Here i dont know how to pass the picture part .
Regards
Mrugen

You shouldn't use HTTP GET because the possible length of the parameters is very limited.
Use POST instead. Here's a link to an example

<?php
$i= 0; // counter
$url = "http://post.jagran.com/rss/post/category/Bollywood.xml"; // url to parse
$rss = simplexml_load_file($url); // XML parser
// RSS items loop
print '<h2><img style="vertical-align: middle;" src="'.$rss->channel->image->url.'" /> '.$rss->channel->title.'</h2>'; // channel title + img with src
foreach($rss->channel->item as $item) {
if ($i < 10) { // parse only 10 items
print ''.$item->title.'<br />';
print ''.$item['image']->url.'<br />';
}
$i++;
}

Related

OG Tags via php

I'm trying to make a very simple game.
a) I let the user input his name;
b) I put this name in a canvas;
c) I generate an image (base64) whit canvas.toDataRL() and I create an image file sending the base64 URI to a php file. I'm doing it with this code:
JAVASCRIPT:
var canvas = document.getElementById("myCanvas");
var dataURL = canvas.toDataURL("image/jpeg", 0.2);
//console.log(dataURL);
// post the dataUrl to php
$.ajax({
type: "POST",
url: "upload.php",
data: {image: dataURL}
}).done(function( respond ) {
console.log(respond);
});
UPLOAD.PHP
<?php
if ( isset($_POST["image"]) && !empty($_POST["image"]) ) {
$dataURL = $_POST["image"];
$parts = explode(',', $dataURL);
$data = $parts[1];
$data = base64_decode($data);
// create a temporary unique file name
$file = "img/" . UPLOAD_DIR . uniqid() . '.png';
// write the file to the upload directory
$success = file_put_contents($file, $data);
print $success ? $file : 'Unable to save this image.';
}
?>
d) Now I'd like to use this image that I've created to set the og:image tag (to share it on Facebook)! How can I do? I've honestly no idea.
Thank you!
Maybe in the OG you can put the link with <?php echo $file; ?>
I've found out the solution.
I pass the respond via get to another php page that catch the get parameter and put it in the og:image'content part.
You can debug your OG markup here: https://developers.facebook.com/tools/debug/
General information about open graph markup: https://developers.facebook.com/docs/sharing/webmasters#markup

Joomla setRedirect doesn't work

I have a simple Joomla controller, but I can't redirect anything.
According to the documentation:
class MyController extends MyBaseController {
function import() {
$link = JRoute::_('index.php?option=com_foo&ctrl=bar');
$this->setRedirect($link);
}
}
//The url contains & html escaped character instead of "&"
This should work, but I get a malformed URL. Is there something I'm missing here? Why is Joomla converting all the "&" characters into &'s? How am I suppose to use setRedirect?
Thank you
Alright, I fixed it. So if anyone needs it:
instead of
$link = JRoute::_('index.php?option=com_foo&ctrl=bar');
$this->setRedirect($link);
use
$link = JRoute::_('index.php?option=com_foo&ctrl=bar',false);
$this->setRedirect($link);
to make it work.
Glad you found your answer, and by the way, the boolean parameter in JRoute::_() is by default true, and useful for xml compliance. What it does is that inside the static method, it uses the htmlspecialchars php function like this: $url = htmlspecialchars($url) to replace the & for xml.
Try this.
$mainframe = &JFactory::getApplication();
$mainframe->redirect(JURI::root()."index.php?option=com_foo&ctrl=bar","your custom message[optional]","message type[optional- warning,error,information etc]");
After inspecting the Joomla source you can quickly see why this is happening:
if (headers_sent())
{
echo "<script>document.location.href='" . htmlspecialchars($url) . "';</script>\n";
}
else
{
... ... ...
The problem is that your page has probably already output some data (via echo or some other means).
In this situation, Joomla is programmed to use a simple javascript redirect. However, in this javascript redirect it has htmlspecialchars() applied to the URL.
A simple solution is to just not use Joomlas function and directly write the javascript in a way that makes more sense:
echo "<script>document.location.href='" . $url . "';</script>\n";
This works for me :)
/libraries/joomla/application/application.php
Find line 400
// If the headers have been sent, then we cannot send an additional location header
// so we will output a javascript redirect statement.
if (headers_sent())
{
echo "<script>document.location.href='" . htmlspecialchars($url) . "';</script>\n";
}
replace to
// If the headers have been sent, then we cannot send an additional location header
// so we will output a javascript redirect statement.
if (headers_sent())
{
echo "<script>document.location.href='" . $url . "';</script>\n";
}
This works!

Displaying images in atom feed

I have problems with displaying images in atom file. It doesn't include images in feed in google reader, opera or firefox.
As a starting point I did everything like in Listing 6. at [An overview of the Atom 1.0 Syndication Format] But it doesn't work.
Update
It is not problem with hotlink protected images. Described here: How to display item photo in atom feed?
Later I changed feed according to description posted here.
I added:
<media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="path_to_image.jpg" />
But still it doesn't work
I had the same problem when trying to include images as enclosure, but it seemed that the easiest way for me was to include the image with the normal img tag to the html content.
(It's also wrapped in CDATA, which might affect the way Google Reader handles the content. I haven't tried without.)
The following example works for me to make atom feed images visible in Google Reader:
<content type="html">
<![CDATA[
<a href="http://test.lvh.me:3000/listings/341-test-pics?locale=en">
<img alt="test_pic" src="http://test.lvh.me:3000/system/images/20/medium/test_pic.jpg?1343246102" />
</a>
]]>
</content>
Wordpress uses the metafield enclosure to set the medias. This is the correct tag according to RSS specification. I have seen people suggest using media:content but if using that make sure to set the XML namespace for it.
Unfortunately due to some dodgy Wordpress code you can not set this dynamically. (Wordpress gets all metafields and then loops through them instead of calling the enclosure directly)
You can set the enclosure on save post. It should be an array with entries of the form "$url\n$length\n$type"
If you want to add the enclosure tags yourself you can do the following:
RSS
add_action( 'rss2_item', 'hughie_rss2_item_enclosure' );
function hughie_rss2_item_enclosure():void
{
$id = get_post_thumbnail_id();
$url = wp_get_attachment_url($id);
$length = filesize(get_attached_file($id));
$type = get_post_mime_type($id);
echo apply_filters( 'rss_enclosure', '<enclosure url="' . esc_url( $url ) . '" length="' . absint( $length ) . '" type="' . esc_attr( $type ) . '" />' . "\n" );
}
ATOM:
add_action( 'atom_entry', 'hughie_atom_entry_enclosure' );
function hughie_atom_entry_enclosure():void
{
$id = get_post_thumbnail_id();
$url = wp_get_attachment_url($id);
$length = filesize(get_attached_file($id));
$type = get_post_mime_type($id);
echo apply_filters( 'atom_enclosure', '<link rel="enclosure" href="' . esc_url( $url ) . '" length="' . absint( $length ) . '" type="' . esc_attr( $type ) . '" />' . "\n" );
}
The only way I found to set the enclosure dynamically is short-circuiting the get_metadata call. You can add checks to make sure that you are in a feed or even the check the stacktrace to make sure.
add_filter('get_post_metadata', 'hughie_get_post_metadata', 10, 5 );
function hughie_get_post_metadata($value, int $object_id, string $meta_key, bool $single, string $meta_type)
{
if (is_feed() && $meta_key === '') {
$backtrace = debug_backtrace();
if (isset($backtrace[7]['function']) && ( $backtrace[7]['function'] === 'rss_enclosure' || $backtrace[7]['function'] === 'atom_enclosure' ) ) {
if (!isset($value['enclosure'])) {
$value['enclosure'] = [];
}
$id = get_post_thumbnail_id();
$url = wp_get_attachment_url($id);
$length = filesize(get_attached_file($id));
$type = get_post_mime_type($id);
$value['enclosure'][] = "$url\n$length\n$type";
}
}
return $value;
}

Tumblr API get current AVATAR URL

Everyone knows? about avatar url in tumblr api / read / json?
like for example the facebook?
http://graph.facebook.com/[your facebook id]/picture?type=normal
<?php
$tumblog = 'natadec0c0'; // change to your username
// if your Tumblog is self hosted, you need to change the base url to the location of your tumblog
$baseurl = 'http://' . $tumblog . '.tumblr.com';
$request = $baseurl . '/api/read/json';
$ci = curl_init($request);
curl_setopt($ci,CURLOPT_RETURNTRANSFER, TRUE);
$input = curl_exec($ci);
curl_close($ci);
// Tumblr JSON doesn't come in standard form, some str replace needed
$input = str_replace('var tumblr_api_read = ','',$input);
$input = str_replace(';','',$input);
// parameter 'true' is necessary for output as PHP array
$value = json_decode($input,true);
$content = $value['posts'];
$blogInfo = $value['tumblelog'];
// the number of items you want to display
$item = 10;
// Echo the blog info
echo "<h3>" . $blogInfo['title'] . "</h3>\n";
echo "<h4>" . $blogInfo['picture'] . "</h4>\n<hr />\n";
?>
how to append my current avatar?
A better solution for this is to put the Avatar api in an img tag.
api.tumblr.com/v2/blog/{base-hostname}/avatar[/size]
example: <img src='http://api.tumblr.com/v2/blog/myreallycoolblog.tumblr.com/avatar/48'/>
So as long as you have the blogname, you can display the avatar.
I guess you have to use
GET http://www.tumblr.com/api/authenticate?email=user#example.com&password=12345
to get the avatar of the user. The sample response for mine is
<tumblr version="1.0">
<user default-post-format="html" can-upload-audio="1" can-upload-aiff="1" can-ask-question="1" can-upload-video="1" max-video-bytes-uploaded="26214400" liked-post-count="134"/>
<tumblelog title="ABNKKPGPiCTuReNPLaKo?!" is-admin="1" posts="301" twitter-enabled="0" draft-count="0" messages-count="0" queue-count="" name="arvn" url="http://arvn.tumblr.com/" type="public" followers="17" avatar-url="http://28.media.tumblr.com/avatar_b1786ec9e62d_128.png" is-primary="yes" backup-post-limit="30000"/>
<tumblelog title="i kras yu." is-admin="1" posts="1" twitter-enabled="0" draft-count="0" messages-count="0" queue-count="" name="ikrasyu" url="http://ikrasyu.tumblr.com/" type="public" followers="2" avatar-url="http://25.media.tumblr.com/avatar_02a7ef66fce8_128.png" backup-post-limit="30000"/>
</tumblr>
and get the avatar-url field of the corresponding tumblelog. Too bad there is no json format option, maybe use preg_match. You also need the email address and password of the user, or do it via OAuth.
Or you could scrape the tumblelog for the avatar.
$page = file_get_contents("http://{$tumblog}.tumblr.com/");
$avatar = preg_match('/<img src="(http.+)" alt="portrait"/', $page, $matches) ? $matches[1]: 'http://example.com/blank.png';

Twitter RSS feed, [domdocument.load]: failed to open stream:

i'm using the following:
<?php
$doc = new DOMDocument();
$doc->load('http://twitter.com/statuses/user_timeline/XXXXXX.rss');
$arrFeeds = array();
foreach ($doc->getElementsByTagName('item') as $node) {
$itemRSS = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue
);
array_push($arrFeeds, $itemRSS);
}
for($i=0;$i<=3;$i++) {
$tweet=substr($arrFeeds[$i]['title'],17);
$tweetDate=strtotime($arrFeeds[$i]['date']);
$newDate=date('G:ia l F Y ',$tweetDate);
if($i==0) { $b='style="border:none;"'; }
$tweetsBox.='<div class="tweetbox" ' . $b . '>
<div class="tweet"><p>' . $tweet . '</p>
<div class="tweetdate">#' . $newDate .'</div>
</div>
</div>';
}
return $tweetsBox;
?>
to return the 4 most recent tweets from a given timeline (XXXXX is the relevant feed)
It seems to work fine but i've recently been getting the following error sporadically:
PHP error debug
Error: DOMDocument::load(http://twitter.com/statuses/user_timeline/XXXXXX.rss) [domdocument.load]: failed to open stream: HTTP request failed! HTTP/1.1 502 Bad Gateway
I've read that the above code is dependant on Twitter beign available and I know it gets rather busy sometimes. Is there either a better way of receiving twits, or is there any kind of error trapping i could do to just to display "tweets are currently unavailable..." ind of message rather than causing an error. I'm usnig ModX CMS so any parse error kills the site rather than just ouputs a warning.
thanks.
I know this is old, but I was just searching for the same solution for a nearly identical script for grabbing a twitter timeline. I ended up doing this, though I haven't been able to thoroughly test it.
I defined the twitter url as a variable ($feedURL), which I also used in $doc_load. Then, I wrapped everything except for the $feedURL into this conditional statement:
$feedURL = "http://twitter.com/statuses/user_timeline/XXXXXXXX.rss"
$headers = #get_headers($feedURL);
if (preg_match("/200/", $headers[0])){
//the rest of you original code in here
}
else echo "Can't connect user-friendly message (or a fake tweet)";
So, it's just checking the headers of the the feed's page, and if its status is 200 (OK), then the rest of the script will execute. Otherwise, it'll echo a message of your choice.
(reference: http://www.phptalk.com/forum/topic/3940-how-to-check-if-an-external-url-is-valid-andor-get-file-size/ )
ETA: Or even better, save a cached version of the feed (which will also ensure you don't go over your API limit of loads):
<?php
$cache_file = dirname(__FILE__).'/cache/twitter_cache.rss';
// Start with the cache
if(file_exists($cache_file)){
$mtime = (strtotime("now") - filemtime($cache_file));
if($mtime > 600) {
$cache_rss = file_get_contents('http://twitter.com/statuses/user_timeline/75168146.rss');
$cache_static = fopen($cache_file, 'wb');
fwrite($cache_static, $cache_rss);
fclose($cache_static);
}
echo "<!-- twitter cache generated ".date('Y-m-d h:i:s', filemtime($cache_file))." -->";
}
else {
$cache_rss = file_get_contents('http://twitter.com/statuses/user_timeline/75168146.rss');
$cache_static = fopen($cache_file, 'wb');
fwrite($cache_static, $cache_rss);
fclose($cache_static);
}
//End of caching
?>
Then use $cache_file in your $doc->load($cache_file) statement instead of the actual feed url.
(Adapted from here: http://snipplr.com/view/8156/twitter-cache/).