I've been working on a website, and I need to know how to get the name of a project with only the URL of the project in PHP. Does anybody know how to do this?
Offline
Use JS by id or something?
Offline
jji7skyline wrote:
Use JS by id or something?
See, that's the thing, it's meant to be done server-side. A user submits a URL and information is collected and added to the database. It seems Sparks knows how to do this, he did it in his API. (the whole latest project thing)
Offline
it's done with the scratchr api's I'll see if I can find what you're looking for...
Offline
Eternity_Inc wrote:
it's done with the scratchr api's I'll see if I can find what you're looking for...
Thank you!
Offline
ok, I've found something. I have to figure out how to decode the results in php though.
but, the api is this:
http://scratch.mit.edu/api/getprojectinfobyid/[IDHERE]
Offline
Eternity_Inc wrote:
ok, I've found something. I have to figure out how to decode the results in php though.
but, the api is this:
http://scratch.mit.edu/api/getprojectinfobyid/[IDHERE]
Thanks! I think I know how to decode it.
But I'm new to PHP; how do you put the value it returns into a variable?
Offline
Gravitation wrote:
But I'm new to PHP; how do you put the value it returns into a variable?
Nevermind, google worked.
Offline
Here you go
<?php //return the name of a project using the URL //written by Sparks for Gravitation. No credit necessary. $project = 'http://scratch.mit.edu/projects/sparks/2047220'; // change this variable to set the project link. You can also use $project = $_GET['project']; and then put "?project=PROJECTNAMEHERE" on the end of your page URL $projectId = substr($project, strpos($project, "/", 33) + 1); //get the project ID from the URL by reading all the characters after the last "/" in the string. $result = file_get_contents("http://scratch.mit.edu/api/getprojectinfobyid/" . $projectId); // uses the Scratch API to get project info for the ID. (full API documentation can be found here: http://trac.assembla.com/scratchr/browser/app/controllers/api_controller.php //get the project name, which is listed between the first and second set of ":"'s in the response. $result = substr($result, strpos($result, ":") + 1); //clear the text before the first ":" $result = substr($result, 0, strpos($result, ":"));//clear the text after the second ":" $result = urldecode($result); // replaces url encoded characters. E.g. "%20" is replaced with " ". echo $result; // your project name. ?>
I hope this helps, the code works, I've tested it, ask if you have more questions
P.S. here's a copy of the code without my many comments:
<?php $project = 'http://scratch.mit.edu/projects/sparks/2047220'; $projectId = substr($project, strpos($project, "/", 33) + 1); $result = file_get_contents("http://scratch.mit.edu/api/getprojectinfobyid/" . $projectId); $result = substr($result, strpos($result, ":") + 1); $result = substr($result, 0, strpos($result, ":")); $result = urldecode($result); echo $result; ?>
Last edited by sparks (2012-11-02 10:38:11)
Offline
fg123 wrote:
Another possibility in the future, is that if you see the URL, you can set a variable with file_get_contents to that particular url, then simply echo out the variable to see how the URL is set.
Mmm. The problem here is that there isn't an API for Scratch that displays only the project name, just a page with lots of info about a project such as this:
183006:athlete%27s%20foot%20powder:This%20is%20an%20ACCIDENTAL%20VIDEO%2C%20why%20not%20check%20out%20my%20website%3A%0A%0Awww.accidentalgames.com%0A%0Afor%20more%20hilarity%3F%0A%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%0A%0AThanks%20to%20WindowsExplorer%20for%20curating%21%2029%2F11%2F11%0A%0A%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%0A%0ATo%20celebrate%20my%20friend%27s%2019th%20birthday%20I%20animated%20one%20of%20his%20comedy%20sketches%20and%20this%20is%20what%20I%20got%21%0A%20%0ACongrats%20to%20SSBBM%20and%20rookwood101%20for%20correctly%20guessing%20the%20name%20of%20the%20%22Top%20scientist%22%20in%20this%20video%20and%20which%20series%20he%20comes%20from%21%20%28Dr.%20Membrane%20from%20the%20Invader%20Zim%20series%29%21%0A%0ACongrats%20to%20Rookwood101%20for%20being%20the%20first%20person%20who%20worked%20out%20what%20the%20joke%20is%20on%20Crazyman%27s%20t-shirt.%20%28It%27s%20a%20hypotemoose%2C%20by%20the%20way%21%29%0A%0AAll%20art%2C%20animation%20and%20voices%20are%20my%20own%20excepting%20the%20muscle%20man.:2011-09-25%2014%3A54%3A08:story%2Cdung%20beetle%20costume%21%21%21%21%21%3A%2Cathletes%20foot%20powder%21%21%21%2Cmens%20lipstick%21%21%2Chahahahaha%2Ci%20want%20a%20dung%20beetle%20cost%2Ccostume%2Cfree%20chocolate%20toaster%2Cdung%20beetle%20costume%3F%3F%2Cgimme%20costume%2Cdung%20beetles%20are%2Cirrelevant%2Cawesome:GB:64:30:2:2:12:17:235:41
If you look, you can see that the project name is listed in between the first and second ":", so a little code is needed to filter the other text out from either side after the file_get_contents() is done.
@mythbusteranimator: Sorry if it looks complex, I tried to comment the code as much as possible...
Last edited by sparks (2012-11-02 12:26:28)
Offline
sparks wrote:
fg123 wrote:
Another possibility in the future, is that if you see the URL, you can set a variable with file_get_contents to that particular url, then simply echo out the variable to see how the URL is set.
Mmm. The problem here is that there isn't an API for Scratch that displays only the project name, just a page with lots of info about a project such as this:
Code:
183006:athlete%27s%20foot%20powder:This%20is%20an%20ACCIDENTAL%20VIDEO%2C%20why%20not%20check%20out%20my%20website%3A%0A%0Awww.accidentalgames.com%0A%0Afor%20more%20hilarity%3F%0A%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%0A%0AThanks%20to%20WindowsExplorer%20for%20curating%21%2029%2F11%2F11%0A%0A%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%0A%0ATo%20celebrate%20my%20friend%27s%2019th%20birthday%20I%20animated%20one%20of%20his%20comedy%20sketches%20and%20this%20is%20what%20I%20got%21%0A%20%0ACongrats%20to%20SSBBM%20and%20rookwood101%20for%20correctly%20guessing%20the%20name%20of%20the%20%22Top%20scientist%22%20in%20this%20video%20and%20which%20series%20he%20comes%20from%21%20%28Dr.%20Membrane%20from%20the%20Invader%20Zim%20series%29%21%0A%0ACongrats%20to%20Rookwood101%20for%20being%20the%20first%20person%20who%20worked%20out%20what%20the%20joke%20is%20on%20Crazyman%27s%20t-shirt.%20%28It%27s%20a%20hypotemoose%2C%20by%20the%20way%21%29%0A%0AAll%20art%2C%20animation%20and%20voices%20are%20my%20own%20excepting%20the%20muscle%20man.:2011-09-25%2014%3A54%3A08:story%2Cdung%20beetle%20costume%21%21%21%21%21%3A%2Cathletes%20foot%20powder%21%21%21%2Cmens%20lipstick%21%21%2Chahahahaha%2Ci%20want%20a%20dung%20beetle%20cost%2Ccostume%2Cfree%20chocolate%20toaster%2Cdung%20beetle%20costume%3F%3F%2Cgimme%20costume%2Cdung%20beetles%20are%2Cirrelevant%2Cawesome:GB:64:30:2:2:12:17:235:41If you look, you can see that the project name is listed in between the first and second ":", so a little code is needed to filter the other text out from either side after the file_get_contents() is done.
@mythbusteranimator: Sorry if it looks complex, I tried to comment the code as much as possible...
That's exactly what I did, it works like a charm. You'll have to use urldecode() though.
$projectname = file_get_contents("http://scratch.mit.edu/api/getprojectinfobyid/" . $projectid); $projectname = explode(":", $projectname); $projectname = $projectname[1]; $projectname = urldecode($projectname);
It works perfectly.
Offline
I used urldecode too, if you noticed Wow, looks like you made that much neater than me. I've never used the explode() function - time to go read up on it, I think!
EDIT: Oh wow. I've just realized how much smaller I can make my api code now...
Last edited by sparks (2012-11-02 13:49:38)
Offline
sparks wrote:
I used urldecode too, if you noticed Wow, looks like you made that much neater than me. I've never used the explode() function - time to go read up on it, I think!
EDIT: Oh wow. I've just realized how much smaller I can make my api code now...
Ah.
Offline
sparks, you're going to want to use rawurldecode(); instead
Offline