简体   繁体   English

如何使用curl在API中使用数组

[英]How to use an array in an API using curl

I am trying to access the cdnify API to purge cache for an individual file ( https://cdnify.com/learn/api#purgecache ) 我正在尝试访问cdnify API来清除单个文件的缓存( https://cdnify.com/learn/api#purgecache

This is my current code 这是我当前的代码

    $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);

the env variables at the top call in my api key, password, and resource for the url. 我的api键,密码和网址资源顶部调用的env变量。 I have verified I am logging in via that url. 我已经验证我通过该URL登录。

When I debug through my code i get an error on 当我调试代码时,出现错误

    $fields = array(
        'files' => $storageFilename
    );

which is Array to string conversion . 这是Array to string conversion

The $storageFilename variable returns $storageFilename变量返回

$storageFilename = "/" . $directoryname . "/" . $asset->name;

which is the filename required for the API call of DELETE. 这是DELETE的API调用所需的文件名。

I can't get passed that $fields array. 我无法通过该$ fields数组。 The other stuff below it may or may not run properly. 下面的其他内容可能会或可能无法正常运行。 I am just stuck on how to write this part out. 我只是停留在如何写这部分。

CURLOPT_POST is just there to indicate if some post data should be included in the HTTP request, so its value should a boolean ( true or false ). CURLOPT_POST只是用来指示HTTP请求中是否应包含某些发布数据,因此其值应为布尔值( truefalse )。

If your array $fields represents the data to be posted, you need to use http_build_query() to assign them to CURLOPT_POSTFIELDS : 如果数组$fields表示要发布的数据,则需要使用http_build_query()将它们分配给CURLOPT_POSTFIELDS

curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));

There is a return in your code that stops the code the curl code is not getting executed remove it or comment it using // and try again and CURLOPT_POST value is boolean true or false that indicates if you want to use post method or not, your CURL code is really messed up you want to use http delete method or post method ?? 您的代码中有一个return ,用于停止未执行curl代码的代码,请删除它,或使用//注释它,然后重试,并且CURLOPT_POST值为布尔值true或false,指示您是否要使用post方法,您的CURL代码真的搞砸了,您想使用http delete方法还是post方法? You can only use one method, please learn how to use php cURL first http://php.net/manual/en/book.curl.php 您只能使用一种方法,请先学习如何使用php cURL http://php.net/manual/zh/book.curl.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM