NOTE : the url I posted here was purposely broken by putting a space after the http:// due to the strict rules of stackoverflow.
If you enter this url
https:// graph.facebook.com/v2.4/?fields=share{comment_count}&id=http:// kclau.com/investment/where-klci-is-heading/
directly in browser, it displays :
{ "share": {
"comment_count": 3 }, "id": "http:// kclau.com/investment/where-klci-is-heading/" }
Fine. So I prepare the php as follow :
<?php
$url = "http:// kclau.com/investment/where-klci-is-heading/";
$response = file_get_contents("https:// graph.facebook.com/v2.4/?fields=share{comment_count}&id=".$url);
$obj = json_decode($response);
echo $obj->share->comment_count;
?>
It display nothing at all.
I tried var_dump($obj) display NULL !!
What's wrong with it ? Can someone help please ? thanks
I am not sure why the previous code not working, but someone had suggested to me the CURL, which is working :-
<?php
$url = "http://kclau.com/investment/where-klci-is-heading/";
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, "https://graph.facebook.com/v2.4/?fields=share{comment_count}&id=".$url);
$result = curl_exec($ch);
curl_close($ch);
$obj = json_decode($result);
echo $obj->share->comment_count;
?>
Related
So i am trying to get values of json object from a url, when i hit that url on post man i get something like this
{
"error": "0",
"errorString": "",
"reply": {
"nonce": "5e415334832a8",
"realm": "VMS"
}
}
So, i am trying to write a php code that displays the value of nonce in the browser but it is not working
i have the following code
$getNonceUrl = "https://example.com/api/getNonce";
$getContect = file_get_contents($getNonceUrl);
$jsonNoce = json_decode($getContect, true);
$dd = $jsonNoce->reply[0]->nonce;
echo $dd;
I also did this
$ch = curl_init("https://example.com/api/getNonce");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
echo $ch->reply[0]->nonce;
But it does not seem to work still.
Below your 2 code samples with suggested corrections.
Using file_get_contents : remove 2nd argument of json_decode
$getNonceUrl = "https://example.com/api/getNonce";
$getContect = file_get_contents($getNonceUrl);
$jsonNoce = json_decode($getContect); // note removal of 2nd parameter
$dd = $jsonNoce->reply[0]->nonce;
echo $dd;
Using curl : you forgot to parse curl output with json_decode
$ch = curl_init("https://example.com/api/getNonce");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
$data = json_decode($data); // added this line, because curl doesn't decode json automatically
echo $ch->reply[0]->nonce;
First part of the question solved. Please look at EDIT 2
I have the following problem:
I get a username out of my database with a htmlspecialchar, in this case: exámple. I parse it via cUrl to the Riot-Games API.
$url = 'https://euw.api.pvp.net/api/lol/euw/v1.4/summoner/by-name/exámple?api_key=<my-api-key>'
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
return $output;
curl_close($ch);
My result is: { "status": { "message": "Not Found", "status_code": 404 } }
But the user exists! I can open the URL in my browser and it works...
I think cUrl can't parse the á in the string, if i copy the url and open it in browser
á is converted to %C3%A1
Is it somehow possible to convert the URL in PHP to a sendable request?
Thanks in advance ;)
EDIT
If i use ex%C3%A1mple instead of exámple in my user-database it works!
EDIT 2
I now use urlencode to the username. However I'm despairing...
My simple script:
echo urlencode($row["username"]) . " " . urlencode('exámple');
The $row["username"] outputs me guaranteed 'exámple'. It's an SQL request.
The output i get:
ex%E1mple ex%C3%A1mple
Solved:
urlencode(utf8_encode($row["username"]))
use urlencode function to encode name. See below code:
$name='exámple';
$encoded_name = urlencode('exámple'); //output: ex%C3%A1mple
$url = 'https://euw.api.pvp.net/api/lol/euw/v1.4/summoner/by-name/'.$encoded_name.'?api_key=<my-api-key>';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
echo $output;
Below is the code I am using
$decode = json_decode($json, true);
var_dump($decode);
This results in the below:
c.ak.instagram.com/hphotos-ak-xfa1/10666256_719752088073218_1127882203_a.jpg"
["full_name"]=> string(26) "Promote OLShop Harga Murah" ["bio"]=> string(0) "" ["id"]=>
string(9) "356515767" } } } }
How do I get Get Media ID?example results :
817757393383064097_356515767
Please help me.
$json = file_get_contents('https://api.instagram.com/v1/tags/gaul/media/recent?access_token=1463408808.e757b44.0738048e481448b48f1cbb23f70f0195&count=1');
$decode = json_decode($json, true);
$media_id = $decode['data'][0]['id']
If you took a look at Embedding you would find a suitable answer, for me i was in need for the media_id for a project, so i wrapped it into a function
function getMediaID($permalink) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.instagram.com/oembed?url=' . $permalink);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$oembed = curl_exec($ch);
curl_close($ch);
$oembed = json_decode($oembed, true);
return $oembed['media_id'];
}
That result you showed are when getting a USER data... not medias. To get media use some like:
https://api.instagram.com/v1/users/USERID/media/recent/?access_token=TOKEN&count=COUNT
Where:
USERID ir the user you want to get medias
TOKEN is your access token that enable you to use the API
COUNT to tell how much photos of that user you want to retrieve at this time
code by Bankzilla is working perfectly. I would upvote it , but for my reputation points are less :/. Also the code isnt echoing anything so the page will be blank.
Here is the code to display the media id. Copy it into a file with php extension for ex: getmedia.php and paste url where your are hosting it in browser and run it.
ex : www.myhost.com/mysite/getmedia.php?url="URL OF THE MEDIA HERE"
<?php
$permalink = $_GET["url"];
getMediaID($permalink) ;
function getMediaID($permalink) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.instagram.com/oembed?url=' . $permalink);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$oembed = curl_exec($ch);
curl_close($ch);
$oembed = json_decode($oembed, true);
echo $oembed['media_id'];
return $oembed['media_id'];
}
?>
You will use:
$search_response = curlRequest("get", "https://api.instagram.com/v1/users/self/media/recent/?access_token={$json_data['access_token']}");
Then:
$photo_id= $search_response['data'][$i]['id'];
echo $photo_id .'<br/>';
($i for exapmle data[1] , data[2] etc.) - (each image data)
I need a function that return me the Timezone of a specific location, so i use the
Google Time Zone API.
function timezoneLookup($lat, $lng){
$url = 'https://maps.googleapis.com/maps/api/timezone/json?location='.$lat.','.$lng.'×tamp='.time().'&sensor=false';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
The function doesn't work because if i return $url i can see that GET variable "×tamp=" is transformed into "×tamp=".
If i run the script outside the function it works.
WHY??
----UPDATE----
I resolved the problem, the curl doesn't work with https://, so i add:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
See this for more information PHP cURL Not Working with HTTPS
The function works fine. The reason you're seeing ×tamp= is because × is being converted to ×. If you view source you'll see the correct url(instead of viewing the converted entity on the web page).
Why ; is not required
There is no problem with this function. If you echo that URL you will get the multiplication sign because it is being filtered through html and recognizing the ascii code. This only happens when you view it though and html viewer (browser), if you view source you will see the original string.
To confirm that this conversion will not occur when passed through curl_setopt(), I ran your code on my server and got an expected result.
echo timezoneLookup(52.2023913, 33.2023913);
function timezoneLookup($lat, $lng){
$url = 'https://maps.googleapis.com/maps/api/timezone/json?location='.$lat.','.$lng.'×tamp='.time().'&sensor=false';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
Returned...
{ "dstOffset" : 3600, "rawOffset" : 7200, "status" : "OK", "timeZoneId" : "Europe/Kiev", "timeZoneName" : "Eastern European Summer Time" }
If this code is not working for you then it could be a networking issue. Try doing curl with another webpage and see what happens. Also, with a simple api call like this you could easily use file_get_contents()
I found the Bitly API code below on this site. I'm having a hard time getting it to create and then echo a Bitly shortened URL for a variable called $fullurl. How would I do that?
EDIT: No error code appears, just no bitly shortened URL is shown.
EDIT 2: var_dump($response); returns NULL
EDIT 3: I did replace the API login and key with my mine.
EDIT 4: I found the answer in one of the comments on the original tutorial. My question was too basic for all you PHP pros: I simply needed to add echo bitly_shorten($fullurl); at the end.
Thanks in advance,
John
function bitly_shorten($url)
{
$query = array(
"version" => "2.0.1",
"longUrl" => $url,
"login" => API_LOGIN, // replace with your login
"apiKey" => API_KEY // replace with your api key
);
$query = http_build_query($query);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://api.bit.ly/shorten?".$query);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
$response = json_decode($response);
if($response->errorCode == 0 && $response->statusCode == "OK") {
return $response->results->{$url}->shortUrl;
} else {
return null;
}
}
Change it to:
function bitly_shorten($url){
$query = array(
"version" => "2.0.1",
"longUrl" => $url,
"login" => API_LOGIN, // replace with your login
"apiKey" => API_KEY // replace with your api key
);
$query = http_build_query($query);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://api.bitly.com/v3/shorten?".$query);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
$response = json_decode($response);
if( $response->status_txt == "OK") {
return $response->data->url;
} else {
return null;
}
}
It seems that bit.ly had updated their api , please visit
http://code.google.com/p/bitly-api/wiki/ApiDocumentation#Authentication_and_Shared_Parameters
for api..
The url seems to be something like this, http://api.bitly.com/v3/shorten?.....
The new version they stated is 3 and in your code its 2.0.1
Whenever you are using an api of an online service its better to get it from their site instead of getting that from any third party site or blog..
I found the answer in one of the comments on the original tutorial. My question was too basic for all you PHP pros: I simply needed to add echo bitly_shorten($fullurl); at the end.