[英]How to use an array in an API using curl
我正在嘗試訪問cdnify API來清除單個文件的緩存( https://cdnify.com/learn/api#purgecache )
這是我當前的代碼
$cdn_api_user = env('CDNIFY_API');
$cdn_api_password = env('CDNIFY_API_PASS');
$cdn_api_resource = env('CDNIFY_API_RESOURCE');
$cdnifyapicacheurl = 'https://' . $cdn_api_user . ':' . $cdn_api_password . '@' . 'cdnify.com/api/v1/resources/' . $cdn_api_resource . '/cache';
return print $cdnifyapicacheurl;
$fields = array(
'files' => $storageFilename
);
$fields_string = http_build_query($fields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $cdnifyapicacheurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
//unless you have installed root CAs you can't verify the remote server's certificate. Disable checking if this is suitable for your application
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//perform the HTTP DELETE
$result = curl_exec($ch);
//close connection
curl_close($ch);
我的api鍵,密碼和網址資源頂部調用的env變量。 我已經驗證我通過該URL登錄。
當我調試代碼時,出現錯誤
$fields = array(
'files' => $storageFilename
);
這是Array to string conversion
。
$storageFilename
變量返回
$storageFilename = "/" . $directoryname . "/" . $asset->name;
這是DELETE的API調用所需的文件名。
我無法通過該$ fields數組。 下面的其他內容可能會或可能無法正常運行。 我只是停留在如何寫這部分。
CURLOPT_POST
只是用來指示HTTP請求中是否應包含某些發布數據,因此其值應為布爾值( true
或false
)。
如果數組$fields
表示要發布的數據,則需要使用http_build_query()
將它們分配給CURLOPT_POSTFIELDS
:
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
您的代碼中有一個return
,用於停止未執行curl代碼的代碼,請刪除它,或使用//
注釋它,然后重試,並且CURLOPT_POST值為布爾值true或false,指示您是否要使用post方法,您的CURL代碼真的搞砸了,您想使用http delete方法還是post方法? 您只能使用一種方法,請先學習如何使用php cURL http://php.net/manual/zh/book.curl.php
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.