gpt4 book ai didi

PHP curl : Encoding/Parsing (AWS Neptune using SPARQL)

转载 作者:行者123 更新时间:2023-12-05 07:01:13 33 4
gpt4 key购买 nike

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

我有以下三元组:

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

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

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

但是 % 字符引发错误(因为如果我使用 - 而不是 % 它会起作用。)因为 this answer我尝试反斜杠 % 字符:

<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 + 解决方案:

正如 Kelvin Lawrence 所指出的,这很可能是 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 在我对其进行 urlencode 之前将其接收(或解码)为原始字符。因此,如果我在发送之前再次对它进行 urlencode,它就可以正常工作:

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

但是 为什么 Neptune 接收它是解码的,或者它为什么要解码它? 它是在 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#%"#¤%

关于PHP curl : Encoding/Parsing (AWS Neptune using SPARQL),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63922585/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com