0

I am using an API I found online for querying a remote Grin daemon using curl. I am trying to convert the bash curl request below into PHP, but I am not sure how I pass the .api_secret using PHP's curl library.

Bash

$ curl -0 -XPOST -u grin:`cat ~/.grin/floo/.api_secret` --data '{"jsonrpc":"2.0","method":"retrieve_summary_info","params":[true, 10],"id":1}' http://127.0.0.1:13420/v2/owner

The main part of this command I am confused about is as follows:

grin:`cat ~/.grin/floo/.api_secret`

How do I convert the command above into PHP format? This is what I have so far:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://127.0.0.1:13420/v2/owner');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"jsonrpc\":\"2.0\",\"method\":\"retrieve_summary_info\",\"params\":[true, 10],\"id\":1}");
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
return $result;
curl_close($ch);
2
  • use file_get_contents in it's place - php.net/manual/en/function.file-get-contents.php Commented Jul 4, 2019 at 8:32
  • Thanks for your answer @treyBake but could you please elaborate a little, I'm not sure I understand what you mean Commented Jul 4, 2019 at 8:33

1 Answer 1

1

As treyBake says, you can get it by using file_get_contents and sending it to cURL with the CURLOPT_USERPWD option. Like so:

// your path needs to either be relative or full, no '~' allowed
// example: '/home/grin/.grin/floo/.api_secret'
$secret = file_get_contents(PATH);
if (!$secret)
    return;

curl_setopt($ch, CURLOPT_USERPWD, "grin:$secret");
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect, that makes a lot of sense

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.