问题描述
我有一项工作要做,我需要登录该网页并从中提取内容.查询必须包含用户名和访问密钥.问题是我真的不知道如何使用 thos 2 元素和 PHP 进行查询.所以,我发现这个代码有这个代码:
I have a work to do and I need to login to the webpage, and extract content from it.The query has to be made of a username and an access key.The problem is that I don't really know how to make a query with thos 2 elements, and in PHP.So, I have found this code have this code :
$ak = "accesskey";
$username = 'admin';
$password = '123';
$remote_url = 'http://192.168.1.78/index.php';
// Create a stream
$opts = array(
'http'=>array(
'method'=>"GET",
'header' => "Authorization: Basic " . base64_encode("$username:$ak")
)
);
$context = stream_context_create($opts);
// Open the file using the HTTP headers set above
$file = file_get_contents($remote_url, false, $context);
print($file);
但输出只是网页代码,从
But the output is simply the Webpage code, from the <!DOCTYPE html><html>
to </html>
根据网页REST API,我需要得到的结果是这样的:
According to the webpage REST API, the result that I need to get is this :
{
success: true,
result: json_result
}
知道为什么它不起作用吗?
Any idea why it doesn't work ?
PS:这里是 API 文档:https://www.vtiger.com/docs/rest-api-for-vtiger#/Authentication
PS : here is the API documentation : https://www.vtiger.com/docs/rest-api-for-vtiger#/Authentication
推荐答案
vTiger API(至少在 7.2 中)具有参数和表单 POST 有效负载的奇怪组合.这是我用来入门的 Python 脚本.请注意,它在 getchallenge
命中中使用了 param
,但在 login
命中中使用了 data
:
The vTiger API (in 7.2 at least) has an odd combination of parameter and form POST payloads. Here is the Python script I used to get started. Note that it uses param
in the getchallenge
hit but data
in the login
hit:
import requests
from hashlib import md5
import urllib3
urllib3.disable_warnings() # shut up whining about that security stuff
encoding = 'ascii'
userName = 'Michael'
userToken = (access key from my preferences page)
APIURL='https://our.crm.instance/webservice.php'
session = requests.Session()
def getSession(session, name, token):
response = session.get(APIURL, params={'operation':'getchallenge', 'username':userName}, verify=False)
token_key = response.json()['result']['token']
combined = token_key + userToken
accessKey = md5(combined.encode(encoding)).hexdigest()
response1 = session.post(APIURL,
data={'operation':'login', 'username':userName, 'accessKey':accessKey},
verify=False)
return response1.json()['result']['sessionName']
sessionToken = getSession(session, userName, userToken)
types = session.get(APIURL, params={'operation':'listtypes', 'sessionName':sessionToken})
print(types.json())
这篇关于我无法使用 PHP 进行基本的 http 身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!