I'm using CURL to get Youtube page like this:
$url = "https://www.youtube.com/watch?v=jyPnQw_Lqds";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$curl_scraped_page = curl_exec($ch);
curl_close($ch);
echo $curl_scraped_page;
It seems the video doesn't play, I don't know why. I found a script which solves this problem by setting the data as format json and then decodes the json as plain html like this:
$youtube = "http://www.youtube.com/oembed?url=" . $url. "&format=json";
$curl = curl_init($youtube);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$return = curl_exec($curl);
curl_close($curl);
$result = json_decode($return, true);
echo $result['html'];
Im trying to "merge" this two scripts to function as one. I put the scripts together like this:
PHPFiddle EXAMPLE, CLICK THE RUN(F9) BUTTON
As you can see the video in the top doesn't play. If you scroll down to the bottom you will see another video (which is the video echoed by the JSON) this one can actually be played.
How do I get the upper(top) video to play? I was thinking about first "finding" the "object" and then format as json etc. But Im afraid this will mess up the page.
Related
I am trying to fetch data from a URL using cURL...this is something I would be able to do but the URL that I want to fetch data from has a section of php code within it - i.e. the URL is populated using a values generated by an earlier section of code.
I want to access the URL, take the figures out of it and then plug those figures into something else. At the minute I am hitting a bit of a roadblock as the php doesn't populate the URL...any ideas?
The code I am using is:
$url = "https://*START OF API ADDRESS*<?php echo $request_ids; ?>*END OF API ADDRESS*";
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$url);
$result=curl_exec($ch);
curl_close($ch);
//var_dump(json_decode($result, true));
$rates = json_decode($result, true);
// Save result
$rates['last_updated'] = time();
file_put_contents($file, json_encode($rates));
}
I'm new to using and API in php.
I have an existing call to an external API, which works and looks like this:
// $id = "227";
// $url = "external-api.com/places/$id?;
// url above works, too
$url = "external-api.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$partner_code:$secret_code");
$result = curl_exec($ch);
curl_close($ch);
echo($result);
On my local server the page is displayed correctly when I enter http://localhost/mytest/mytest.php
The page also displays links and when I click on of them, it shows me the page could not be found. This is because the target is localhost/places/227 and there is no such page.
What I want now is that a call to localhost/places/227 shows a result of an API call to external-api.com/places/227. Just like in the example above, except that this time $url has changed to external-api.com/places/227.
But this shouldn't happen to every link on my website. Just to those links that were retrieved by the API call.
So I think this is what I want: When the link to /places/227 is clicked I want the above php script to be executed again except that the varaible for $url has changed to $url = "external-api.com/places/227;
I'm setting up a little webservice usable via an app on Android.
The function in the server is this:
$lat=43.0552592;
$lng=12.4483577;
$radius=2000;
$url = "https://maps.googleapis.com/maps/api/place/textsearch/json?query=italian&location=".$lat.",".$lng."&radius=".$raidus."&types=restaurant&sensor=false&key=".$apiKey;
$urlW = "https://maps.googleapis.com/maps/api/place/textsearch/json?query=italian&location=43.0552592,12.4483577&radius=2000&types=restaurant&sensor=false&key=XXXXxxxXXxxXXxxxxXXX";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
// Set so curl_exec returns the result instead of outputting it.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Get the response and close the channel.
$response = curl_exec($ch);
return $response;
The trouble is that if I query Google with the $url (as in the above example) it returns the JSON with 'INVALID REQUEST', but if the query at the first curl_opt is done with $curlW will works like a charm.
While debugging that I've discovered, making $url return, that gets every & converted (before the curl_init!) in &...!
So I've tried almost every PHP string function to force every & decode or replacing every &entities to & only without any result.
Any suggestion?
you've done something wrong,
$url = "https://maps.googleapis.com/maps/api/place/textsearch/json?query=italian&location=".$lat.",".$lng."&radius=".$raidus."&types=restaurant&sensor=false&key=".$apiKey
should be
$url = "https://maps.googleapis.com/maps/api/place/textsearch/json?query=italian&location=".$lat.",".$lng."&radius=".$radius."&types=restaurant&sensor=false&key=".$apiKey;
but i don't guess that is what is doing the problem, try using file_get_contents() instead of curl, so $response will be like this:
$response = file_get_contents($url);
hi I have a small problem i need help with i have a web page that outputs live streams in json format with links looking like this
rtmp:\/\/server:port\/camera1.stream
now if I copy and past that link into notepad and change the \/ to / my self, the links plays fine in every media player I have and I can see the camera feed but when I use a php script to decode the json output the streams do not play at all when outputed by echo or when added to a genarated m3u8 file :
bellow is the code i use to get the page and json decode
$stream_id = $_GET["cam"];
// create curl resource
$ch = curl_init();
//set url
curl_setopt($ch, CURLOPT_URL, "http://ip:port/json");
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string
$output = curl_exec($ch);
//close curl resource to free up system resources
curl_close($ch);
$response = json_decode($output), true);
foreach ($response['cameras'] as $result) {
if($result['name'] == $stream_id){
echo $result['cam_stream'];
}
}
I've been working of a project of mine, just to learn a bit more about PHP and JSON handling. I worked with the Fanart.tv API now and get everything up and running as I want, but I've run into some issues. I want to get the FIRST array in the JSON file, but everything I've tried so far doesn't do the trick.
Anyone wants to help me?
Here's the JSON file I'm trying to work with;
{"Eminem":{"mbid_id":"b95ce3ff-3d05-4e87-9e01-c97b66af13d4","artistbackground":[{"id":"14795","url":"http://assets.fanart.tv/fanart/music/b95ce3ff-3d05-4e87-9e01-c97b66af13d4/artistbackground/eminem-4ed3e35d6f3da.jpg","likes":"3"}
This is my code from my PHP file that actually works;
$img = $json->Eminem->artistbackground[0]->url;
However, I want to print the image and echo the name of the artist - in this case "Eminem". I've tried with "$json[0]" and just "[0]", but then I just get error messages. Anyone know how this can be done, reading the JSON file? I get the script updating the ID in the URL ($mbid) from MusicBrainz automatically, so if it is another artist than Eminem I read the JSON file from - my code doesn't work.
This is my PHP code so far;
$backgroundURL = 'http://api.fanart.tv/webservice/artist/a93e666f14a34d979b3e3617eab2f340/'.$mbid.'/json/artistbackground/1/1';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $backgroundURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; curl)");
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$json = curl_exec($ch);
curl_close($ch);
$json = json_decode($json);
$bg = $json->Eminem->artistbackground[0]->url;
echo '<img src="'.$bg.'" height="540" width="960">'
As long as the JSON file is for the artist Eminem, it works. As soon as I get another artist, it doesn't so i need the first element in the JSON file in the $bg string.
Anyone knows how to do this?
Thanks for the help!
current() function returns value of the current key (first):
$json = json_decode($json);
$first = current($json);
$bg = $first->artistbackground[0]->url;