我做了一个Yii2 REST API.使用API,您可以获得汽车列表.现在我想使用承载身份验证来保护API.但我不知道它是如何工作的.
首先.我在控制器的behavior方法中设置了authenticator.
public function behaviors(){
return [
'contentNegotiator' => [
'class' => ContentNegotiator::className(),
'formats' => [
'application/json' => Response::FORMAT_JSON,
],
],
'authenticator' => [
'class' => CompositeAuth::className(),
'authMethods' => [
HttpBearerAuth::className(),
],
]
];
}
Run Code Online (Sandbox Code Playgroud)
这很好用.如果我转到URL,我将收到"未经授权"的消息.
在我的wordpress插件中,我已经创建了一个函数来使用API并使用身份验证密钥设置标头.
function getJSON($template_url) {
$authorization = "Authorization: Bearer " . get_option("auth_key");
// Create curl resource
$ch = curl_init();
// Set URL
curl_setopt($ch, CURLOPT_URL, $template_url);
// Return transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Set headers
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', $authorization));
// …Run Code Online (Sandbox Code Playgroud) 我正在尝试将我的详细信息页面的标题更改为汽车的名称.我制作了一个插件,用汽车信息填充详细页面.但现在我无法让add_filter('wp_title')在我的插件中运行.
这是我试过的代码:
function init() {
hooks();
}
add_action('wp', 'init');
function hooks() {
if(get_query_var('car_id') != "") {
add_filter('wp_title', 'addTitle', 100);
add_action('wp_head', 'fillHead');
}
add_shortcode('showCar', 'showCar');
}
function addTitle() {
$api_url_klant = API_URL . '/gettitle/' . get_option("ac") . '/' . get_query_var('car_id');
$title = getJSON($api_url_klant);
return $title['merk'] . " " . $title['model'] . " - " . $title['bedrijf'];
}
Run Code Online (Sandbox Code Playgroud)
addTitle()函数工作得很好.它返回正确的名称.add_action('wp_head')也可以.我只是不能让wp_title过滤器无法工作.
我是在错误的时刻执行此过滤器还是我做错了什么?
我正在尝试使用PHP Curl上传多个图像.我正在使用的API给出了以下示例:
curl -v -s -u username:password \
-H "Content-Type: multipart/form-data" \
-H "Accept: application/vnd.com.example.api+json" \
-F "image=@img1.jpeg;type=image/jpeg" \
-F "image=@img2.jpeg;type=image/jpeg" \
-XPUT 'https://example.com/api/sellers/12/ads/217221/images'
Run Code Online (Sandbox Code Playgroud)
所以在我的PHP脚本中我试过这个:
$files = array();
foreach($photos as $photo) {
$cfile = new CURLFile('../' . $photo, 'image/jpeg', 'test_name');
$files[] = $cfile;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://services.example.com/seller-api/sellers/' . $seller . '/ads/' . $voertuig_id . '/images');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $files);
curl_setopt($ch, CURLOPT_PROXY,'api.test.sandbox.example.com:8080');
curl_setopt($ch, CURLOPT_USERPWD, 'USER:PASS');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, …Run Code Online (Sandbox Code Playgroud) 我正在尝试将带有降价标志的 json 字符串发送到 API。如果我发送以下字符串它工作正常:
"## Heading 2 \n * List item \n * List item"
但是如果我使用以下字符串:
"## Heading 2 \n ## Heading 2 \n * List item \n * List item"
它根本不会起作用。第一个 \n 消失了,它的标题如下:
标题 2 ## 标题 2
我需要实现的是:
<p>A paragraph with some text.</p>
<h2>A heading</h2>
<ul>
<li> item 1 </li>
<li> item 1 </li>
</ul>
Run Code Online (Sandbox Code Playgroud) 我用 Yii2 框架制作了一个 API。但我不知道如何在我的语句中使用 OR 条件。
例如:我想要获得所有品牌为 BMW 或 DODGE 的汽车。
我尝试过以下方法:
$query = Car::getCar($lang)
->where(['or', ['m.brand' => 'BMW'], ['m.brand' => 'DODGE']])
->all();
Run Code Online (Sandbox Code Playgroud)
但这行不通。我只能让它与 m.brand 的一个值一起工作。
所以:
$query = Car::getCar($lang)
->where(['m.brand' => 'BMW'])
->all();
Run Code Online (Sandbox Code Playgroud)
效果很好。
尝试过其他一些方法,但我无法使其发挥作用。
有谁知道我做错了什么?
编辑 getCar 方法返回类似以下内容:
(new Query())->select(['a.auto_id'])->from('auto_new a')
Run Code Online (Sandbox Code Playgroud)
编辑2 让它可以使用:
$query->andWhere(['or', ['m.brand' => 'BMW'], ['m.brand' => 'DODGE']])
Run Code Online (Sandbox Code Playgroud)