<p>I found the solution replicating <a href="https://github.com/janoside/btc-rpc-explorer/blob/master/app/api/electrumAddressApi.js" rel="nofollow noreferrer">https://github.com/janoside/btc-rpc-explorer/blob/master/app/api/electrumAddressApi.js</a></p>

<pre><code>$scriptPubKey = '76a914e0ad60c897901128662623c500a4a6079e99cd3e88ac';

$addrScripthash = hash('sha256', hex2bin($scriptPubKey));
$re = '/.{2}/';
preg_match_all($re, $addrScripthash, $matches, PREG_PATTERN_ORDER, 0);
$addrScripthash = implode('', array_reverse($matches[0]));

print($addrScripthash);
</code></pre>

CC BY-SA 4.0
2019-12-09T16:09:27.640Z
2019-12-09T16:09:27.640Z
Gotenks
<p>I have to query addresses in ElectrumX server. How do I generate scripthash for a known scriptPubKey in PHP?</p>

<p>Example address:</p>

<pre><code>array (
  'isvalid' =&gt; true,
  'address' =&gt; '1MUz4VMYui5qY1mxUiG8BQ1Luv6tqkvaiL',
  'scriptPubKey' =&gt; '76a914e0ad60c897901128662623c500a4a6079e99cd3e88ac',
  'isscript' =&gt; false,
  'iswitness' =&gt; false,
)
</code></pre>

<p>I tried:</p>

<pre><code>hash('sha256', hex2bin('76a914e0ad60c897901128662623c500a4a6079e99cd3e88ac'))
</code></pre>

<p>But it seems to be wrong.</p>

<address><electrum><php>
Get scripthash for known scriptPubKey in PHP
Gotenks
# Get scripthash for known scriptPubKey in PHP

(Asked by: username_0 on 2019-12-09)

I have to query addresses in ElectrumX server. How do I generate scripthash for a known scriptPubKey in PHP?

Example address:

```
array (
  'isvalid' => true,
  'address' => '1MUz4VMYui5qY1mxUiG8BQ1Luv6tqkvaiL',
  'scriptPubKey' => '76a914e0ad60c897901128662623c500a4a6079e99cd3e88ac',
  'isscript' => false,
  'iswitness' => false,
)

```

I tried:

```
hash('sha256', hex2bin('76a914e0ad60c897901128662623c500a4a6079e99cd3e88ac'))

```

But it seems to be wrong.

**Tags:** address, electrum, php


---

## 1 answers

### Answer by username_0 on 2019-12-09. Score: 0 | Accepted answer

I found the solution replicating <https://github.com/janoside/btc-rpc-explorer/blob/master/app/api/electrumAddressApi.js>

```
$scriptPubKey = '76a914e0ad60c897901128662623c500a4a6079e99cd3e88ac';

$addrScripthash = hash('sha256', hex2bin($scriptPubKey));
$re = '/.{2}/';
preg_match_all($re, $addrScripthash, $matches, PREG_PATTERN_ORDER, 0);
$addrScripthash = implode('', array_reverse($matches[0]));

print($addrScripthash);

```


---

