使用PHP将DELETE发送到API [英] Sending DELETE to API using PHP

查看:94
本文介绍了使用PHP将DELETE发送到API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是第一次使用API​​包装器之外的API.我可以使用

I am brand new to using an API outside of an API wrapper. I can access the API using

curl -u username:password https://company.c
om/api/v1/resources/xxxxxxx

这将加载所有信息,但是我需要做的是基于文件名数组将DELETE发送到url;例如['/js/jquery.js'].参数的名称是文件".

That loads up all the information, but what I need to do is send a DELETE to the url based on an array of filenames; e.g. ['/js/jquery.js']. The name of the parameter is Files.

我已经在代码中包含目录和文件名变量.

I already have in code the directory and file name variables.

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

上面从数据库返回/directoryname/filename.

Above returns the /directoryname/filename from the database.

推荐答案

使用PHP中的cURL库发送HTTP(S)删除:

To send an HTTP(S) DELETE using the cURL library in PHP:

$url = 'https://url_for_your_api';

//this is the data you will send with the DELETE
$fields = array(
    'field1' => urlencode('data for field1'),
    'field2' => urlencode('data for field2'),
    'field3' => urlencode('data for field3')
);

/*ready the data in HTTP request format
 *(like the querystring in an HTTP GET, after the '?') */
$fields_string = http_build_query($fields);

//open connection
$ch = curl_init();

/*if you need to do basic authentication use these lines,
 *otherwise comment them out (like, if your authenticate to your API
 *by sending information in the $fields, above. */
 $username = 'your_username';
 $password = 'your_password';
 curl_setopt($process, CURLOPT_USERPWD, $username . ":" . $password);
/*end authentication*/

curl_setopt($ch, CURLOPT_URL, $url);
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);

/* this answer builds on David Walsh's very good HTTP POST example at:
 * http://davidwalsh.name/curl-post 
 * modified here to make it work for HTTPS and DELETE and Authentication */

这篇关于使用PHP将DELETE发送到API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆