简体   繁体   English

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

[英]PHP cURL: Encoding/Parsing (AWS Neptune using SPARQL)

I'm new to SPARQL so this might be a dumb question but I'm having troubles inserting data.我是 SPARQL 的新手,所以这可能是一个愚蠢的问题,但我在插入数据时遇到了麻烦。 I'm using the HTTPS REST endpoint to communicate with the DB.我正在使用 HTTPS REST 端点与数据库进行通信。

I have the following triple:我有以下三重:

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

Which I urlencode() because, as I thought, it would give me no errors since it has the proper URI format:我使用urlencode()是因为,正如我所想的那样,它不会给我任何错误,因为它具有正确的 URI 格式:

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

Yet the % character is raising the error (because if I use a - instead of the % it works.) Because of this answer I tried backslashing the % character:然而 % 字符引发了错误(因为如果我使用-而不是%它起作用。)由于这个答案,我尝试反斜杠%字符:

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

Then I tried using a URL instead of the URN:然后我尝试使用 URL 而不是 URN:

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

But it keeps giving me the same error:但它一直给我同样的错误:

The requested URL returned error: 400 Bad Request

So what am I doing wrong?那么我做错了什么? How can I escape the % character (and maybe other urlencode characters?) Or should I ask, Is this the right way of doing it, doing a urlencode() before submitting?我怎样才能转义%字符(也许还有其他 urlencode 字符?)或者我应该问,这是正确的做法吗,在提交之前做一个urlencode()

EDIT:编辑:

my PHP code:我的 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;

Again, if I just change % to - it works:同样,如果我只是将%更改为-它有效:

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

As Kelvin Lawrence pointed out, it's likely it is some parsing/encodigin issue on PHP's side.正如 Kelvin Lawrence 指出的那样,这很可能是 PHP 方面的一些解析/编码问题。 I deleted the CURLOPT_FAILONERROR and now I have these errors showing up:我删除了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"}

It looks like the error using the URN shows that Neptune receives it (or decodes it) as the original character before I urlencoded it.看起来使用 URN 的错误表明海王星在我对它进行 urlencoded 之前将它接收(或解码)作为原始字符。 So if I urlencode it once more before sending it does work as it should:因此,如果我在发送之前再次对其进行 urlencode 确实可以正常工作:

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

But why does Neptune receive it decoded or why does it decode it?但是为什么海王星接收它解码或者为什么它解码它? It is send in the POST body so it doesn't need to be decoded, right?它在 POST 正文中发送,因此不需要解码,对吗? What am I missing?我错过了什么?

EDIT3:编辑3:

Another thing I should mention:还有一点我要提一下:

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

This query works perfectly, while the same query with newlines does not work:此查询完美运行,而带有换行符的相同查询不起作用:

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

It gives me this error:它给了我这个错误:

{
    "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"
}

Why?为什么? I can solve this by keeping the query at one line, but that's not how it should work.我可以通过将查询保留在一行来解决这个问题,但这不是它应该如何工作的。

Try to utilize base64_encode / decode.尝试使用 base64_encode/decode。 Transfering data between php files works for me that way在 php 文件之间传输数据对我有用

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

File B:文件乙:

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

gives:给出:

sadfkjæ4#%"#¤%

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM