PHP cURL:编码/解析(AWS Neptune使用SPARQL)



我是SPARQL的新手,所以这可能是一个愚蠢的问题,但我在插入数据时遇到了问题。我使用HTTPS REST端点与数据库进行通信。

我有以下三个:

<urn:data:Gen`000001>
<urn:data:content>
"This is a literal"

urlencode()是因为,正如我所想,它不会给我任何错误,因为它具有正确的URI格式:

<urn:data:Gen%60000001>
...

然而,%字符引起了错误(因为如果我使用-而不是%,它就起作用了。(因为这个答案,我尝试反斜杠%字符:

<urn:data:Gen%60000001>
...

然后我尝试使用URL而不是URN:

<https://test.com/data/Gen%60000001>
<https://test.com/data/content>
...

但它一直给我同样的错误:

The requested URL returned error: 400 Bad Request

那么我做错了什么?如何转义%字符(以及其他urlencode字符?(或者我应该问,这是正确的方法吗?在提交之前先执行urlencode()

编辑:

我的PHP代码:

$q = 'update=INSERT DATA { '.
'<https://test.com/data/Gen%60000001> '.
'<https://test.com/data/content> '.
'"This is a literal." }';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$config->db->public->neptune->cluster."/sparql");
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2); 
curl_setopt($ch, CURLOPT_TIMEOUT, 3);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $q);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
if (curl_errno($ch)) {
$error = curl_error($ch);
}
if(!$error)
echo $server_output;
else
echo $error;

同样,如果我只是将%更改为-,它会起作用:

query=select * where {?s ?p ?o}
result:
"s" : {
"type" : "uri",
"value" : "https://test.com/data/Gen-60000001"
},
"p" : {
"type" : "uri",
"value" : "https://test.com/data/content"
},
"o" : {
"type" : "literal",
"value" : "This is a literal."
}

EDIT2+解决方案:

正如KelvinLawrence所指出的,这很可能是PHP方面的解析/编码问题。我删除了CURLOPT_FAILONERROR,现在出现了以下错误:

using URN:
{
"detailedMessage":"Malformed query: Lexical error at line 1, column 28. 
Encountered: "`" (96), after : """,
"requestId":"***",
"code":"MalformedQueryException"
}
using URL:
{
"detailedMessage":"Malformed query: Lexical error at line 1, column 32. 
Encountered: "/" (47), after : "test.com"",
"requestId":"***",
"code":"MalformedQueryException"}

看起来使用URN的错误显示Neptune在我对其进行URL编码之前将其作为原始字符接收(或解码(。因此,如果我在发送之前再次对其进行url编码,它确实可以正常工作:

$q = 'update=INSERT DATA { '.
'<https://test.com/data/'.urlencode(urlencode('Gen`60000001')).'> '.
'<https://test.com/data/content> '.
'"This is a literal." }';

但是为什么海王星接收到解码,或者为什么解码它是在POST主体中发送的,所以不需要解码,对吧?我错过了什么?

第3版:

我应该提到的另一件事:

$q = 'query=select * where {?s ?p ?o}';

这个查询工作得很好,而带有换行符的相同查询不起作用:

$q = 'query=select * 
where {
?s
?p 
?o
}';

它给了我这个错误:

{
"detailedMessage":"Malformed query: Encountered "" at line 1, column 9.n
Was expecting one of:n    "{" ...n    "from" ...n    "where" ...n    
"with" ...n    ",
"requestId":"***",
"code":"MalformedQueryException"
}

为什么我可以通过将查询保持在一行来解决这个问题,但这不是它应该如何工作的。

尝试使用base64_encode/decode。在php文件之间传输数据对我来说是这样的

文件A:$string=";sadfkjæ4#%"#¥%";$string_for_transfer=base64_encode($string(;

文件B:

$string = $_GET["string"];
$string_data = base64_decode($string);
echo $string_data;

给出:

sadfkjæ4#%"#¤%

最新更新