0

Not wasting a second, check out this code:

$gp=$_GET["gp"];

function googleplus($gp) {
$furl = "http://query.yahooapis.com/v1/public/yql?q=SELECT * from html where url='https://plus.google.com/%252B".$gp."' AND xpath=\"//div[@class='Nn']//div[@class='rw Uc']//div[@class='Zi']//div[@class='V9b nhe']//div[@class='tQE8Kd BnqoOb']//div[@class='Qhb eZa']//div[@class='vkb']//p\"&format=xml";
$api = simplexml_load_file($furl);
$followers = $api->results->p;
return $followers;
}

Okay. When I do

<?php echo googleplus($gp); ?> 

with parameter gp=mehulmohan in the URL, it should return something like this:

539 have him in circles

Check this URL associated with mehulmohan

http://query.yahooapis.com/v1/public/yql?q=SELECT%20*%20from%20html%20where%20url=%27https://plus.google.com/%252Bmehulmohan%27AND%20xpath=%22//div[@class=%27Nn%27]//div[@class=%27rw%20Uc%27]//div[@class=%27Zi%27]//div[@class=%27V9b%20nhe%27]//div[@class=%27tQE8Kd%20BnqoOb%27]//div[@class=%27Qhb%20eZa%27]//div[@class=%27vkb%27]//p%22&format=xml

But actually, it returns blank. Why?

Please help. All other social media like pinterest and twitter are returning correct results but Google+ returns blank.

Point to note: I've parsed the "+" symbol 2 times in the URL as in 1 time, it is not accepting the XML but second time it takes.

"+" = %2B = %252B

1 Answer 1

1

Encode the characters in the URL properly using urlencode(). And remember to always cast SimpleXML objects to string. The updated function would look like:

function googleplus($gp) {
    $furl = "http://query.yahooapis.com/v1/public/yql?q=SELECT * from html where url='https://plus.google.com/%252B".$gp."' AND xpath=\"//div[@class='Nn']//div[@class='rw Uc']//div[@class='Zi']//div[@class='V9b nhe']//div[@class='tQE8Kd BnqoOb']//div[@class='Qhb eZa']//div[@class='vkb']//p\"&format=xml";
    $api = simplexml_load_file(urlencode($furl));
    $followers = (string) $api->results->p;
    return $followers;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Stackoverflow is saying that You can't accept an answer in 8 minutes. I'll accept your answer as best when this limit is removed. Well, could you tell me what urlencode is doing over there? Thanks again!
@MehulMohan: Since URLs often contain characters outside the ASCII set, the URL has to be converted into a valid ASCII format. URL encoding replaces unsafe ASCII characters with a % followed by two hexadecimal digits. In this case, urlencode() replaces all spaces with + sign. See this demo to see how the URL looks like after applying urlencode().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.