问题描述
我有工作要做,我需要登录到该网页并从中提取内容.查询必须由用户名和访问密钥组成.问题是我真的不知道如何使用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);
但是输出只是网页代码,从<!DOCTYPE html>< html>
到</html>
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#/身份验证
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身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!