jikan-me / jikan

Unofficial MyAnimeList PHP+REST API which provides functions other than the official API
https://jikan.moe
MIT License
875 stars 93 forks source link

php work?? #484

Closed CodingBDX closed 1 year ago

CodingBDX commented 1 year ago

i try to use top manga in vanilla php (not framework) i have install composer install jikan

but how to use it? i follow documentation, not work, not found jikan,nothing work..

thank for answer

pushrbx commented 1 year ago

In case you'd like to just send a request to the API and get the top manga list docs are here: https://docs.api.jikan.moe/#tag/top/operation/getTopManga

In terms of php you are expected to use either a stream wrapper or curl or other third party library to make a http request to the API.

Example with curl:

$ch = curl_init();
// return the transfer as a string of the return value of [curl_exec()](https://www.php.net/manual/en/function.curl-exec.php) instead of outputting it directly to stdout.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// the url where you want to send the request to
curl_setopt($ch, CURLOPT_URL, "https://api.jikan.moe/v4/top/manga");
// turn on compression
curl_setopt($ch, CURLOPT_SSH_COMPRESSION, true);
$result = curl_exec($ch);

// you might have to use json deserialization instead of this, not sure 😄 
$server_response_body = unserialize($result);

curl_close($ch);

print_r($server_response_body);

If you'd like to use the Jikan library to parse MAL pages locally then need to call the getTopManga function with the appropriate request: https://github.com/jikan-me/jikan/blob/21aff2c12fbe28b0da8dcb558c29cd3340e5d080/src/MyAnimeList/MalClient.php#L618

CodingBDX commented 1 year ago

i try to install Jikan library, but it"s not work, what's it's the process?

composer install Jikan? and after? i try install MalClient but not work...

thank for fast answe!r

pushrbx commented 1 year ago

I can't help you there until you share with us the following:

pushrbx commented 1 year ago

btw you only have to composer install jikan-me/jikan and import the autoloader file from vendors folder.

CodingBDX commented 1 year ago

i have install and import, i try to show top list of manga

`require '../vendor/autoload.php';

// require '../models/Manga.php';

use Jikan\MyAnimeList\MalClient;

$jikan = new MalClient();

// Top manga $topManga = $jikan->TopManga(); `

irfan-dahir commented 1 year ago

This is how you make top manga request in Jikan.

$topManga = $jikan->getTopManga(new TopMangaRequest($page, $type))

You need to pass the respective Request class (TopMangaRequest) into the get API class (getTopManga).


Also, this API syntax is invalid: $topManga = $jikan->TopManga();

Can you check if your jikan version is ^v3 https://github.com/jikan-me/jikan/releases/tag/v3.3.2

CodingBDX commented 1 year ago

This is how you make top manga request in Jikan.

$topManga = $jikan->getTopManga(new TopMangaRequest($page, $type))

You need to pass the respective Request class (TopMangaRequest) into the get API class (getTopManga).

Also, this API syntax is invalid: $topManga = $jikan->TopManga();

Can you check if your jikan version is ^v3 https://github.com/jikan-me/jikan/releases/tag/v3.3.2

okey but it's not write in documentation, where???? i try your code $topManga = $jikan->getTopManga(new TopMangaRequest($page, $type)) not found class getTopManga....

i try download zip version 3 but after download and launch composer install, what i do???

thank

irfan-dahir commented 1 year ago

You need to composer require jikan-me/jikan in your project. Please read into using composer. https://metabox.io/composer-and-autoload-in-php/

The docs for jikan php are not upto date. I'd suggest using any IDE with intellisense auto complete like PHPStorm.

CodingBDX commented 1 year ago

Yes i do, it install lot depance but After, i do what? Explain more please or update documentation Friends

pushrbx commented 1 year ago

Personally I had some issues installing jikan with composer version 2.2.6, so after upgrading to 2.4.3 everything worked. Here is a sample which works:
test_jikan.zip

You only have to extract it, and run composer install in the folder.

The steps I did:

mkdir test_jikan
cd test_jikan
composer init

image

The key thing is PSR-4 and you have to import the correct namespaces into context. Which you have to dig for ofcourse in the source code of Jikan.

janvernieuwe commented 1 year ago

Or you can use my api client. Tho that also works with composer. https://github.com/janvernieuwe/jikan-jikanPHP

If you want to do PHP, I highly recomend reading up on composer usage and package management and use an intellisense IDE as @irfan-dahir suggested.

CodingBDX commented 1 year ago

hello friend, i have do the same thing like you, installation are good my file composer.json { "name": "root/manga", "require": { "jikan-me/jikan": "^3.3" }, "license": "MIT", "autoload": { "psr-4": { "Root\\Manga\\": "src/" } }, "authors": [ { "name": "CodingBDX", "email": "" } ] }

and my index.php into src `require '../vendor/autoload.php';

// require '../models/Manga.php';

use Jikan\MyAnimeList\MalClient;

$jikan = new MalClient();

$topManga = $jikan->getTopManga(new TopMangaRequest($page, $type)); `

and i have this error with xdebug Fatal error: Uncaught Error: Class "TopMangaRequest" not found in /var/www/manga/src/index.php on line 11

thank for fast answer friend!

CodingBDX commented 1 year ago

Or you can use my api client. Tho that also works with composer. https://github.com/janvernieuwe/jikan-jikanPHP

If you want to do PHP, I highly recomend reading up on composer usage and package management and use an intellisense IDE as @irfan-dahir suggested.

yes but you can explain, i download ans launch composer init, but after??? you explain not clear...

janvernieuwe commented 1 year ago

Please read up here https://phptherightway.com/#composer_and_packagist I advise you to read up the other topics on the site as well, since it is the best resource on how to learn modern PHP.

pushrbx commented 1 year ago

@CodingBDX

Fatal error: Uncaught Error: Class "TopMangaRequest" not found in /var/www/manga/src/index.php on line 11

Have you tried importing that symbol? Like this:

use Jikan\Request\Top\TopMangaRequest;

Here is your code what you posted with fixes applied:

<?php
namespace CodingBDX \MyMangaApp;
require '../vendor/autoload.php';

// require '../models/Manga.php';

use Jikan\MyAnimeList\MalClient;
use Jikan\Request\Top\TopMangaRequest;

$jikan = new MalClient();
$topManga = $jikan->getTopManga(new TopMangaRequest(1, "manga"));

print_r($topManga);

If this works you will owe me two sets of Krispy Kreme donuts and a coffee. 😸 Also the error message literally tells you what you are doing wrong. So I advise you too to read more about modern PHP on that website what janvernieuwe has suggested. 🌈

CodingBDX commented 1 year ago

thank you for your helps, it's work for topmangarequest ;)

now, i try to convert the result of json top a array, but it's return 'TypeError: file_get_contents(): Argument #1 ($filename) must be of type string'

`$topMangas = $jikan->getTopManga(new TopMangaRequest(1, 'manga'));

// converte top json in php

$response = json_decode(file_get_contents($topMangas), true);`

pushrbx commented 1 year ago

The contents of the $topMangas is an instance of the TopManga class. You need to convert it to an array object to be able to export it as json. file_get_contents is a function to read files from the disk. It takes a file pointer or a file path as first argument. You need to use the following function to convert the TopManga class instance to an array: https://github.com/jikan-me/jikan/blob/master/src/Model/Top/TopManga.php#L70

CodingBDX commented 1 year ago

thank man i do this $result = $topMangas->getResults(); and i call into foreach <body> <?php foreach ($result as $key => $manga) { echo $manga['title']; } ?>

but not work nnot use object of type Jikan\Model\Top\TopMangaListItem as array in /var/www/html/manga/public/index.php o

pushrbx commented 1 year ago

Each element of the array is a TopMangaListItem instance. https://github.com/jikan-me/jikan/blob/master/src/Model/Top/TopMangaListItem.php

Accordingly you need to use the getter functions to get each field.

pushrbx commented 1 year ago

Please don't forget about my Krispy Kremes and coffee: https://ko-fi.com/pushrbx

CodingBDX commented 1 year ago

you can show me one exemple, i try ->getTitle() but it' don't find method..

pushrbx commented 1 year ago

Using my previous example as a base:

<?php
namespace Pushrbx\TestJikan;
require "vendor/autoload.php";

use Jikan\MyAnimeList\MalClient;
use Jikan\Request\Top\TopMangaRequest;

$jikan = new MalClient();
$topManga = $jikan->getTopManga(new TopMangaRequest(1, "manga"));
$results = $topManga->getResults();
print_r($results[0]->getTitle());
CodingBDX commented 1 year ago

i understand friend, that work, thank for your support!