[英]PHP cURL Post API
我正在嘗試使用BlockCypher API接受以太坊。 從他們的文檔( https://dev.blockcypher.com/eth/#address-endpoint ),他們要求發送一個cURL請求以獲取地址。
我已經試過了:-
<?php
$a = "https://api.blockcypher.com/v1/eth/main/addrs?token=my_token";
$b = file_get_contents($a);
var_dump($b);
?>
這給了我這個錯誤:-
Warning: file_get_contents(https://api.blockcypher.com/v1/eth/main/addrs?token=my_token): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in C:\wamp\www\x\backend\dev\t.php on line 3
如果我嘗試直接在Web上運行URL,則會收到此錯誤:-
Endpoint not found. Please check your URL for typos and make sure you're using the correct HTTP method (GET, POST, etc).
我在做什么錯?
cURL請求通過cURL庫完成 。 它們很可能會阻止來自其他客戶端的請求,這些請求未(通過User-Agent字符串)標識為cURL。 您正在使用file_get_contents,這很容易,但帶有“ PHP”用戶代理字符串。
有關更多信息,請參見PHP文檔中的cURL示例 。
編輯:現在在我看來您讀錯了API文檔。 GET方法沒有終結點/addrs
地址,您鏈接到的文檔“比地址平衡終結點返回有關地址交易的更多信息,但犧牲了一些響應速度”。 要生成地址,您必須POST到該API的generate-adress端點 。
試試下面的代碼,它將起作用:
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.blockcypher.com/v1/eth/main/addrs/738d145faabb1e00cf5a017588a9c0f998318012",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_SSL_VERIFYHOST =>0,
CURLOPT_SSL_VERIFYPEER =>0,
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.