我如何 return 来自 php 的特定数组数据?
How do I return specific array data from php?
我正在使用 async/await 获取一个文件 (createCharge.php),当在 checkout.html
上单击 <button>
时,该文件包含包含内部数据的数组
如果我使用 print_r
我可以在控制台中看到所有结果,例如名称、描述、数量,甚至是新生成的 'hosted_url'
客户将使用它来继续付款。
问题是我不知道如何将 hosted_url 结果提取到结帐页面和 <a href="">Pay now!</a>
。
这就是我的 checkout.html...
<button id="btn">Pay with Crypto?</button>
<p id="pay"></p>
<script>
btn.addEventListener('click', async (e) => {e.preventDefault();
const response = await fetch('coinbasePHPtest/createCharge.php/');
if (!response.ok) {
const errorMessage = await response.text();
console.error(response.status, response.statusText, errorMessage);
alert('There was an error creating the charge.');
return;
}
const newurl = await response.text();
console.log(newurl);
pay.innerHTML = `<a href="${newurl}">Pay Now! - Coinbase</a>`;
});
</script>
你可以看到我正在尝试将 hosted_url 放入 <script>
内的 <a>
标签中。
这是我的 createCharge.php,它实际上创建了 hosted_url...(示例:https://commerce.coinbase.com/charges/xxxxx)
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.commerce.coinbase.com/charges');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(
array (
'name' => 'My-Company',
'description' => 'Selected Product',
'local_price' =>
array (
'amount' => '147.00',
'currency' => 'GBP',
),
'pricing_type' => 'fixed_price',
'metadata' =>
array (
'customer_id' => 'id_1',
'customer_name' => 'Satoshi Nakamoto',
),
'redirect_url' => 'https://www.my-site.co.uk/Checkout/payment_successful.php',
'cancel_url' => 'https://www.my-site.co.uk/Checkout/payment_cancelled.php',
)
));
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'X-Cc-Api-Key: MY-API-KEY';
$headers[] = 'X-Cc-Version: 2018-03-22';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
curl_close($ch);
$response = json_decode($result, true);
return $response->data->hosted_url;
?>
我也试过了..
pay.innerHTML = `<a href="${hosted_url}">Pay Now!</a>`
pay.innerHTML = `<a href="{hosted_url}">Pay Now!</a>`
pay.innerHTML = `<a href="${newurl->hosted_url}">Pay Now!</a>`
pay.innerHTML = `<a href="{.hosted_url}">Pay Now!</a>`
运气不好。
和...
btn.onclick = async() => {
const response = await fetch('coinbasePHPtest/charge.php/hosted_url');
const data = await response.json();
&
btn.onclick = async() => {
const resp = await fetch(`coinbasePHPtest/createCharge.php/${hosted_url}`);
const data = await resp.json();
解法:
$result = curl_exec($ch);
curl_close($ch);
$response = json_decode($result, true);
echo $response['data']['hosted_url'];
createCharge.php 中的这段代码对我有用! :)
我想你只想输出数据而不是使用 return。替换
return $response->data->hosted_url;
和
echo $response['data']['hosted_url'];
我正在使用 async/await 获取一个文件 (createCharge.php),当在 checkout.html
上单击<button>
时,该文件包含包含内部数据的数组
如果我使用 print_r
我可以在控制台中看到所有结果,例如名称、描述、数量,甚至是新生成的 'hosted_url'
客户将使用它来继续付款。
问题是我不知道如何将 hosted_url 结果提取到结帐页面和 <a href="">Pay now!</a>
。
这就是我的 checkout.html...
<button id="btn">Pay with Crypto?</button>
<p id="pay"></p>
<script>
btn.addEventListener('click', async (e) => {e.preventDefault();
const response = await fetch('coinbasePHPtest/createCharge.php/');
if (!response.ok) {
const errorMessage = await response.text();
console.error(response.status, response.statusText, errorMessage);
alert('There was an error creating the charge.');
return;
}
const newurl = await response.text();
console.log(newurl);
pay.innerHTML = `<a href="${newurl}">Pay Now! - Coinbase</a>`;
});
</script>
你可以看到我正在尝试将 hosted_url 放入 <script>
内的 <a>
标签中。
这是我的 createCharge.php,它实际上创建了 hosted_url...(示例:https://commerce.coinbase.com/charges/xxxxx)
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.commerce.coinbase.com/charges');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(
array (
'name' => 'My-Company',
'description' => 'Selected Product',
'local_price' =>
array (
'amount' => '147.00',
'currency' => 'GBP',
),
'pricing_type' => 'fixed_price',
'metadata' =>
array (
'customer_id' => 'id_1',
'customer_name' => 'Satoshi Nakamoto',
),
'redirect_url' => 'https://www.my-site.co.uk/Checkout/payment_successful.php',
'cancel_url' => 'https://www.my-site.co.uk/Checkout/payment_cancelled.php',
)
));
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'X-Cc-Api-Key: MY-API-KEY';
$headers[] = 'X-Cc-Version: 2018-03-22';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
curl_close($ch);
$response = json_decode($result, true);
return $response->data->hosted_url;
?>
我也试过了..
pay.innerHTML = `<a href="${hosted_url}">Pay Now!</a>`
pay.innerHTML = `<a href="{hosted_url}">Pay Now!</a>`
pay.innerHTML = `<a href="${newurl->hosted_url}">Pay Now!</a>`
pay.innerHTML = `<a href="{.hosted_url}">Pay Now!</a>`
运气不好。
和...
btn.onclick = async() => {
const response = await fetch('coinbasePHPtest/charge.php/hosted_url');
const data = await response.json();
&
btn.onclick = async() => {
const resp = await fetch(`coinbasePHPtest/createCharge.php/${hosted_url}`);
const data = await resp.json();
解法:
$result = curl_exec($ch);
curl_close($ch);
$response = json_decode($result, true);
echo $response['data']['hosted_url'];
createCharge.php 中的这段代码对我有用! :)
我想你只想输出数据而不是使用 return。替换
return $response->data->hosted_url;
和
echo $response['data']['hosted_url'];