This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2012-11-02 07:28:01

Gravitation
New Scratcher
Registered: 2012-09-26
Posts: 500+

PHP and Project Names

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

 

#2 2012-11-02 07:31:19

jji7skyline
Scratcher
Registered: 2010-03-08
Posts: 1000+

Re: PHP and Project Names

Use JS by id or something?  hmm


I don't know why you say goodbye, I say hello!  big_smile

Offline

 

#3 2012-11-02 07:33:16

Gravitation
New Scratcher
Registered: 2012-09-26
Posts: 500+

Re: PHP and Project Names

jji7skyline wrote:

Use JS by id or something?  hmm

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

 

#4 2012-11-02 07:59:24

Eternity_Inc
Scratcher
Registered: 2011-12-07
Posts: 100+

Re: PHP and Project Names

it's done with the scratchr api's I'll see if I can find what you're looking for...

Offline

 

#5 2012-11-02 08:01:43

Gravitation
New Scratcher
Registered: 2012-09-26
Posts: 500+

Re: PHP and Project Names

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!  smile

Offline

 

#6 2012-11-02 08:14:24

Eternity_Inc
Scratcher
Registered: 2011-12-07
Posts: 100+

Re: PHP and Project Names

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

 

#7 2012-11-02 08:21:19

Gravitation
New Scratcher
Registered: 2012-09-26
Posts: 500+

Re: PHP and Project Names

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

 

#8 2012-11-02 08:25:57

Gravitation
New Scratcher
Registered: 2012-09-26
Posts: 500+

Re: PHP and Project Names

Gravitation wrote:

But I'm new to PHP; how do you put the value it returns into a variable?

Nevermind, google worked.  tongue

Offline

 

#9 2012-11-02 10:36:52

sparks
Community Moderator
Registered: 2008-11-05
Posts: 1000+

Re: PHP and Project Names

Here you go  smile

Code:

<?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  smile

P.S. here's a copy of the code without my many comments:

Code:

<?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)


http://img541.imageshack.us/img541/7563/scratchbetabanner.png

Offline

 

#10 2012-11-02 10:42:21

mythbusteranimator
Scratcher
Registered: 2012-02-28
Posts: 1000+

Re: PHP and Project Names

*dies from looking at PHP mumbo jumbo*


http://www.foxtrot.com/comics/2012-04-01-fdb37077.gif
clicky

Offline

 

#11 2012-11-02 11:33:49

fg123
Scratcher
Registered: 2008-11-13
Posts: 1000+

Re: PHP and Project Names

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.  smile


Hai.

Offline

 

#12 2012-11-02 12:25:44

sparks
Community Moderator
Registered: 2008-11-05
Posts: 1000+

Re: PHP and Project Names

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.  smile

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: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)


http://img541.imageshack.us/img541/7563/scratchbetabanner.png

Offline

 

#13 2012-11-02 13:12:08

Gravitation
New Scratcher
Registered: 2012-09-26
Posts: 500+

Re: PHP and Project Names

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.  smile

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: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...

That's exactly what I did, it works like a charm. You'll have to use urldecode() though.

Code:

$projectname = file_get_contents("http://scratch.mit.edu/api/getprojectinfobyid/" . $projectid);
$projectname = explode(":", $projectname);
$projectname = $projectname[1];
$projectname = urldecode($projectname);

It works perfectly.  big_smile

Offline

 

#14 2012-11-02 13:40:08

sparks
Community Moderator
Registered: 2008-11-05
Posts: 1000+

Re: PHP and Project Names

I used urldecode too, if you noticed  tongue  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)


http://img541.imageshack.us/img541/7563/scratchbetabanner.png

Offline

 

#15 2012-11-02 13:56:52

Gravitation
New Scratcher
Registered: 2012-09-26
Posts: 500+

Re: PHP and Project Names

sparks wrote:

I used urldecode too, if you noticed  tongue  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.  tongue

Offline

 

#16 2012-11-02 15:58:57

Eternity_Inc
Scratcher
Registered: 2011-12-07
Posts: 100+

Re: PHP and Project Names

sparks, you're going to want to use rawurldecode(); instead

Offline

 

#17 2012-11-02 15:58:59

sparks
Community Moderator
Registered: 2008-11-05
Posts: 1000+

Re: PHP and Project Names

(I moved the discussion about explode() being used for the API code over to the API topic  smile )

Last edited by sparks (2012-11-02 15:59:09)


http://img541.imageshack.us/img541/7563/scratchbetabanner.png

Offline

 

Board footer