justpush-io/php-jokeapi 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

justpush-io/php-jokeapi

最新稳定版本:v1.0

Composer 安装命令:

composer require justpush-io/php-jokeapi

包简介

The missing piece in php, a php wrapper for the famous jokes API

README 文档

README

A PHP wrapper for the JokeAPI.

Installation

Currently, this package is not available on Packagist. To use it, simply clone this repository:

git clone https://github.com/yourusername/jokeapi-php.git

Usage

<?php

require_once 'path/to/JokeAPI/JokeClient.php';

use JokeAPI\JokeClient;
use JokeAPI\JokeConstants;

// Create a new JokeClient
$client = new JokeClient();

// Get a random joke
$joke = $client->fetch();

// Print the joke
if ($joke['type'] === 'single') {
    echo $joke['joke'];
} else {
    echo "Setup: " . $joke['setup'] . "\n";
    echo "Delivery: " . $joke['delivery'];
}

Features

The JokeAPI PHP client supports all the features of the JokeAPI:

  • Multiple categories
  • Blacklist flags
  • Different formats (JSON, XML)
  • Joke types (single, twopart)
  • Search functionality
  • Specific joke IDs
  • Multiple languages
  • Safe mode

Methods

categories(array $categories)

Set the categories for the jokes.

$client->categories([
    JokeConstants::CATEGORY_PROGRAMMING,
    JokeConstants::CATEGORY_MISC
]);

Available categories:

  • CATEGORY_ANY (default)
  • CATEGORY_MISC
  • CATEGORY_PROGRAMMING
  • CATEGORY_DARK
  • CATEGORY_PUN
  • CATEGORY_SPOOKY
  • CATEGORY_CHRISTMAS

blacklist(array $flags)

Set blacklist flags to filter jokes.

$client->blacklist([
    JokeConstants::FLAG_NSFW,
    JokeConstants::FLAG_RELIGIOUS
]);

Available flags:

  • FLAG_NSFW
  • FLAG_RELIGIOUS
  • FLAG_POLITICAL
  • FLAG_RACIST
  • FLAG_SEXIST
  • FLAG_EXPLICIT

format(string $format)

Set the response format (json or xml).

$client->format('xml');

type(string $type)

Set the joke type (single or twopart).

$client->type(JokeConstants::TYPE_SINGLE);

Available types:

  • TYPE_SINGLE
  • TYPE_TWOPART

search(string $searchString)

Search for jokes containing the specified string.

$client->search('programmer');

id(int $id)

Get a joke by ID.

$client->id(42);

language(string $language)

Set the language for the jokes.

$client->language(JokeConstants::LANG_GERMAN);

Available languages:

  • LANG_ENGLISH (default)
  • LANG_CZECH
  • LANG_GERMAN
  • LANG_SPANISH
  • LANG_FRENCH
  • LANG_PORTUGUESE

safe(bool $safe = true)

Enable safe mode (excludes nsfw jokes).

$client->safe();

fetch()

Fetch a joke from the API.

$joke = $client->fetch();

Chaining

All methods (except fetch()) return the client instance for method chaining:

$joke = $client
    ->categories([JokeConstants::CATEGORY_PROGRAMMING])
    ->blacklist([JokeConstants::FLAG_NSFW])
    ->safe()
    ->fetch();

License

MIT

Credits

This is a PHP port of the jokeapi Go package by Icelain.

统计信息

  • 总下载量: 6
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-04-18