[英]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.