我有此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错误?
感谢您的帮助。