php - webapi 在 php(和 perl)中发生 405 错误
问题描述
我有这个 API 的 WEB API 和 Perl 语言示例代码。
我想在 PHP 中使用这个 API。
但是,会发生 405 错误。
API 文档(仅登录)
JSON文字列
FIELD TYPE
*username String
*password String
*rememberMe String
示例 perl 代码
use strict;
use DBI;
use LWP::UserAgent;
#認証
my $auth_api = 'http://testapi.com/api';
my $username = 'user';
my $password = 'pass';
my $ua = LWP::UserAgent->new;
$ua->parse_head(0);
#ログイン
my $req = HTTP::Request->new(POST => $auth_api);
$req->content_type('application/json');
$req->content('{'
.'"username":"'.$username.'",'
.'"password":"'.$password.'" '
.'}');
my $res = $ua->request($req);
#トークン取得
my $csrf_access_token = '';
my $access_token_cookie = '';
my $cookie = "";
if($res->is_success) {
print $res->content;
print $res->as_string;
$cookie = $res->header('set-cookie');
my @cookies = split(/,|;/, $cookie);
while (my $c = shift @cookies){
my ($key, $value) = split(/=/, $c);
if($key =~ /csrf_access_token/){
$csrf_access_token = $value;
}
if($key =~ /access_token_cookie/){
$access_token_cookie = $value;
}
}
}
else {
print $res->status_line, "\n";
print $res->content, "\n";
}
print '"'.$csrf_access_token.'"',"\n";
print '"'.$access_token_cookie.'"',"\n";
#検索url
my $search_api = 'http://testapi.com/api';
my $img_url = 'https://test.com/pics/';
my $num_result = '10';
#トークンを使ってアクセスする
if($csrf_access_token ne ''){
my $req2 = HTTP::Request->new(POST => $search_api);
my $shinaban1 = 'g3717380';
open(my $out, '>out_search.tab');
$req2->content_type('application/x-www-form-urlencoded');
$req2->header("Cookie"=>"access_token_cookie=$access_token_cookie; csrf_access_token=$csrf_access_token");
$req2->content(
'img_url='.$img_url.lc($shinaban1).'.jpg'.'&'
.'num_result='.$num_result.''
);
print $req2->as_string;
my $res = $ua->request($req2);
if($res->is_success) {
print $out $res->content;
print $res->content;
}
else {
print $res->headers_as_string;
print $res->status_line, "\n";
print $res->content, "\n";
}
}
示例 perl 代码运行结果
C:\Users\TEST>perl search.pl
{"msg":"Login successful","status":200}
HTTP/1.1 200 OK
Connection: close
Date: Thu, 30 Aug 2018 08:47:16 GMT
Server: nginx/1.10.3 (Ubuntu)
Content-Length: 40
Content-Type: application/json
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: Content-Type,Authorization,X-CSRF-Token
Access-Control-Allow-Methods: GET,PUT,POST,DELETE
Access-Control-Allow-Origin: http://testapi.com
Client-Date: Thu, 30 Aug 2018 08:46:19 GMT
Client-Peer: .........:8080
Client-Response-Num: 1
Set-Cookie: access_token_cookie=qBchXPttiGHhFDuXhi5pQjqN89LTleivPZwQ4eC0; HttpOnly; Path=/api/
Set-Cookie: csrf_access_token=f32a..........388d; Path=/
{"msg":"Login successful","status":200}
"............."
"eyJ0eXAiOiJKV1Q............."
POST http://testapi.com/api
Content-Type: application/x-www-form-urlencoded
Cookie: access_token_cookie=eyJ0eXAiOiJKV1QiLC....4eC0; csrf_access_token=f32a5c82-3a44-44
img_url=https://testapi.com/apig3717380.jpg&num_result=10
{
"status": 0,
"total_time": 235,
"preprocess_time": 11,
"get_feature_time": 99,
"calculate_euclide": 0,
"load_embeddings": 0,
"find_max": 0,
"sort_min": 0,
"post_process_img": "/9j/4AAQSkZCAgUEBAMEBgUGBBggLDAsKDAkKCgr/2wBDAQICAgICAZ",
"response": [
{
"id": 113677,
"name": "test",
"shban1": "ZO413",
"distance": "0.22493",
"url": "https://.......",
"index": 0
},
...
{
"id": 807506,
"name": "DT039",
"shinaban1": "G08327",
"distance": "0.28992",
"url": "https://...............",
"index": 9
}
]
}
我的 php 代码(仅登录部分)
$url = 'http://testapi.com/api';
$username = 'user';
$password = 'pass';
$rememberMe = true;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, $username.":".$password);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json', 'Content-Type: application/json'));
$output = curl_exec($ch);
我的 php 代码运行结果
>>> $ch = curl_init();
=> curl resource #481
>>> curl_setopt($ch, CURLOPT_URL, $url);
=> true
>>> curl_setopt($ch, CURLOPT_USERPWD, $username.":".$password);
=> true
>>> curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json', 'Content-Type: application/json'));
=> true
>>> $output = curl_exec($ch);
{
"message": "The method is not allowed for the requested URL."
}
=> true
webapi 网址信息
>>> $info = curl_getinfo($ch);
=> [
"url" => "http://testapi.com/api",
"content_type" => "application/json",
"http_code" => 405,
"header_size" => 415,
"request_size" => 163,
"filetime" => -1,
"ssl_verify_result" => 0,
"redirect_count" => 0,
"total_time" => 0.187,
"namelookup_time" => 0.125,
"connect_time" => 0.156,
"pretransfer_time" => 0.156,
"size_upload" => 0.0,
"size_download" => 70.0,
"speed_download" => 374.0,
"speed_upload" => 0.0,
"download_content_length" => 70.0,
"upload_content_length" => -1.0,
"starttransfer_time" => 0.172,
"redirect_time" => 0.0,
"redirect_url" => "",
"primary_ip" => "..........",
"certinfo" => [],
"primary_port" => 8080,
"local_ip" => "............",
"local_port" => 5544,
]
我对 Perl 一无所知。
我的 PHP 代码有什么问题?为什么会出现405错误?
提前感谢您的帮助。
解决方案
推荐阅读
- java - 如何清除资源泄漏?
- json - 当键中的点在超过 2 个过滤器字段中时,JSONPath 语法不起作用
- python - python程序崩溃后如何重新启动
- azure-devops - 从项目 azure devops REST API 获取所有工作项
- python-3.x - 从 first 运行的其他脚本访问实例化类
- python - 如何在 Python 中获取浮点数的字节
- reporting-services - 列组中的 SSRS 分页符
- string - 在字幕文件 (srt) 的某些行前面添加字符串
- c - 显示文件或文件夹不存在的 mpi.h 错误
- python - 带有管道的 Bash 和 Python 信号行为不一致