Jump to content

Call function inside quotations marks


Go to solution Solved by moylin,

Recommended Posts

I am writing out likes, tweets and so on in Word Press.
I am struggling to make my code work.

 

Below is the url that my PHP is writing out likes from. (If NBA.com has 20 K likes it will write "20k")

 

<? $obj=new shareCount("http://www.nba.com"); ?>

 

My problem is that the url has to different for each post. 
<?php the_permalink(); ?> Vil write out the URL

My problem is my formatting.

 

<? $obj=new shareCount("<?php the_permalink(); ?>"); ?> of course doesn't work

<? $obj=new shareCount(<?php the_permalink(); ?>); ?> of course doesn't work

How du I call a function inside a function within the quotations marks?

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/278464-call-function-inside-quotations-marks/
Share on other sites

Sorry, half asleep on this.

 

Try

<?php

$obj=new shareCount( get_permalink() );

?>

or

<?php

$yourtempvar = get_permalink();

$obj=new shareCount($yourtempvar);

?>

 

edit: switched to get instead of the_permalink, as the_permalink is designed to output the permalink address to the browser.

Edited by moylin

"<?php $obj = new ShareCount( the_permalink() ); ?> should work if you have the function working in the same scope and have the function return an URL via return."

 

Still just wrting out the URL

http://creeeative.com

Edited by danielnn

<?
class shareCount {
private $url,$timeout;
function __construct($url,$timeout=10) {
$this->url=rawurlencode($url);
$this->timeout=$timeout;
}
function get_tweets() { 
$json_string = $this->file_get_contents_curl('http://urls.api.twitter.com/1/urls/count.json?url=' . $this->url);
$json = json_decode($json_string, true);
return isset($json['count'])?intval($json['count']):0;
}
function get_fb() {
$json_string = $this->file_get_contents_curl('http://api.facebook.com/restserver.php?method=links.getStats&format=json&urls='.$this->url);
$json = json_decode($json_string, true);
return isset($json[0]['total_count'])?intval($json[0]['total_count']):0;
}


function get_pinterest() {
$return_data = $this->file_get_contents_curl('http://api.pinterest.com/v1/urls/count.json?url='.$this->url);
$json_string = preg_replace('/^receiveCount\((.*)\)$/', "\\1", $return_data);
$json = json_decode($json_string, true);
return isset($json['count'])?intval($json['count']):0;
}
private function file_get_contents_curl($url){
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
$cont = curl_exec($ch);
if(curl_error($ch))
{
die(curl_error($ch));
}
return $cont;
}
}
?>



<div class="socialbuttons">
<?php $obj = new ShareCount( the_permalink() ); ?>
<a href=https://www.facebook.com/sharer/sharer.php?u="<?php the_permalink(); ?>" target=_blank><span class=black>
<? echo "".$obj->get_fb();?>
</span><span class=light>Likes</span></a>


<a href=https://www.facebook.com/sharer/sharer.php?u="<?php the_permalink(); ?>" target=_blank><span class=black>
<? echo "".$obj->get_tweets(); ?>
</span><span class=light>Tweets</span></a>


<a href=https://www.facebook.com/sharer/sharer.php?u="<?php the_permalink(); ?>" target=_blank><span class=black>
<? echo "".$obj->get_pinterest(); ?>
</span><span class=light>Pins</span></a>
</div>
Edited by danielnn
  • Solution

Did you try the updated code I put in with the get_permalink() instead of the_permalink.

the_permalink is not designed for what you're trying to do, you have to use get_permalink instead. When i first pasted teh example, i didn't know the difference but udpated it shortly after.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.