我需要在PHP中使用Twilio API来获取我帐户中所有购买的号码,以便可以在HTML表单的<select>
选项中使用它们。
示例:
<select class="form-control">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
有人可以展示如何执行此操作的基本示例吗?
答案 0 :(得分:2)
Twilio API文档非常详尽,并提供了如何进行大部分调用的示例。它们还提供了一个SDK库,使其更加简单。我相信您正在寻找的电话是/2010-04-01/Accounts/{AccountSid}/IncomingPhoneNumbers
(Twilio Documentation)。有一个在该页面调用它的例子:
<?php
// Get the PHP helper library from twilio.com/docs/php/install
require_once('/path/to/twilio-php/Services/Twilio.php'); // Loads the library
// Your Account Sid and Auth Token from twilio.com/user/account
$sid = "ACdc5f132a3c49700934481addd5ce1659";
$token = "{{ auth_token }}";
$client = new Services_Twilio($sid, $token);
// Loop over the list of numbers and echo a property for each one
foreach ($client->account->incoming_phone_numbers as $number) {
echo $number->phone_number;
}
您需要获取“twilio-php”库,该库也可以在他们的网站上找到,或here。
答案 1 :(得分:0)
我这样做是:
public class ActionBlock<TInput1, TInput2> : IDataflowBlock
{
private readonly ITargetBlock<(int, TInput1, TInput2)> _actionBlock;
public Task Completion => _actionBlock.Completion;
public void Complete() => _actionBlock.Complete();
void IDataflowBlock.Fault(Exception ex) => _actionBlock.Fault(ex);
public ITargetBlock<TInput1> Target1 { get; }
public ITargetBlock<TInput2> Target2 { get; }
public ActionBlock(Func<TInput1, Task> action1, Func<TInput2, Task> action2,
ExecutionDataflowBlockOptions options = null)
{
if (action1 == null) throw new ArgumentNullException(nameof(action1));
if (action2 == null) throw new ArgumentNullException(nameof(action2));
options = options ?? new ExecutionDataflowBlockOptions();
_actionBlock = new ActionBlock<(int, TInput1, TInput2)>(entry =>
{
var (index, item1, item2) = entry;
return index switch // switch expression (C# 8.0 syntax)
{
1 => action1(item1),
2 => action2(item2),
_ => throw new NotImplementedException()
};
}, options);
this.Target1 = new TargetConverter<TInput1, (int, TInput1, TInput2)>(
_actionBlock, x => (1, x, default), () => Complete(1));
this.Target2 = new TargetConverter<TInput2, (int, TInput1, TInput2)>(
_actionBlock, x => (2, default, x), () => Complete(2));
}
// Constructor with synchronous lambdas
public ActionBlock(Action<TInput1> action1, Action<TInput2> action2,
ExecutionDataflowBlockOptions options = null) : this(
item1 => { action1(item1); return Task.CompletedTask; },
item2 => { action2(item2); return Task.CompletedTask; }, options) { }
// Complete when both targets complete
private readonly bool[] _completeState = new bool[2];
private void Complete(int index)
{
bool completed;
lock (_completeState.SyncRoot)
{
_completeState[index - 1] = true;
completed = _completeState.All(v => v);
}
if (completed) _actionBlock.Complete();
}
}
// Generic class for converting from one type of ITargetBlock to another
public class TargetConverter<TFrom, TTo> : ITargetBlock<TFrom>
{
private readonly ITargetBlock<TTo> _parent;
public readonly Func<TFrom, TTo> _convert;
public readonly Action _completeAction;
public TargetConverter(ITargetBlock<TTo> parent, Func<TFrom, TTo> convert,
Action completeAction = null)
{
if (parent == null) throw new ArgumentNullException(nameof(parent));
if (convert == null) throw new ArgumentNullException(nameof(convert));
_parent = parent;
_convert = convert;
_completeAction = completeAction;
}
Task IDataflowBlock.Completion => _parent.Completion;
void IDataflowBlock.Complete()
{
if (_completeAction != null) _completeAction(); else _parent.Complete();
}
void IDataflowBlock.Fault(Exception ex) => _parent.Fault(ex);
DataflowMessageStatus ITargetBlock<TFrom>.OfferMessage(
DataflowMessageHeader messageHeader, TFrom messageValue,
ISourceBlock<TFrom> source, bool consumeToAccept)
{
return _parent.OfferMessage(messageHeader,
_convert(messageValue),
source != null ? new SourceProxy(source, this) : null,
consumeToAccept);
}
// An internal ISourceBlock facade is also needed
private class SourceProxy : ISourceBlock<TTo>
{
private readonly ISourceBlock<TFrom> _source;
private readonly TargetConverter<TFrom, TTo> _target;
public SourceProxy(ISourceBlock<TFrom> source,
TargetConverter<TFrom, TTo> target)
{
_source = source;
_target = target;
}
TTo ISourceBlock<TTo>.ConsumeMessage(
DataflowMessageHeader messageHeader,
ITargetBlock<TTo> target,
out bool messageConsumed)
{
return _target._convert(_source.ConsumeMessage(messageHeader,
_target, out messageConsumed));
}
bool ISourceBlock<TTo>.ReserveMessage(
DataflowMessageHeader messageHeader,
ITargetBlock<TTo> target)
{
return _source.ReserveMessage(messageHeader, _target);
}
void ISourceBlock<TTo>.ReleaseReservation(
DataflowMessageHeader messageHeader,
ITargetBlock<TTo> target)
{
_source.ReleaseReservation(messageHeader, _target);
}
Task IDataflowBlock.Completion => throw new NotSupportedException();
void IDataflowBlock.Complete() => throw new NotSupportedException();
void IDataflowBlock.Fault(Exception exception)
=> throw new NotSupportedException();
IDisposable ISourceBlock<TTo>.LinkTo(
ITargetBlock<TTo> target,
DataflowLinkOptions linkOptions) => throw new NotSupportedException();
}
}
与其他两个发布的内容完全不同!我尝试了这些,但他们并没有接近工作。也许我有其他版本的API或php库。
Twilio的文档也说要使用$twilio=new Client(TWILIO_ACC_ID,TWILIO_AUTH_TOKEN);
//get all the phone numbers on account (assuming there won't be more than 1000)
$incoming_phone_numbers=$twilio->incomingPhoneNumbers->read([],1000);
//loop through all numbers
foreach ($incoming_phone_numbers as $number) {
print("<p>sid: ".$number->sid." phone number: ".$number->phoneNumber."</p>");
}
,但这显然是错误的。它肯定会以$number->phone_number
答案 2 :(得分:-1)
Twilio开发者传道者在这里。
您可以致电Incoming Phone Numbers list resource获取帐户中的号码列表。这是在PHP中使用Twilio PHP helper library完成的,如下所示:
<?php
// Get the PHP helper library from twilio.com/docs/php/install
require_once('/path/to/twilio-php/Services/Twilio.php'); // Loads the library
// Your Account Sid and Auth Token from twilio.com/user/account
$sid = "{{ account_sid }}";
$token = "{{ auth_token }}";
$client = new Services_Twilio($sid, $token);
// Loop over the list of numbers and echo a property for each one
foreach ($client->account->incoming_phone_numbers as $number) {
echo $number->phone_number;
}
我会留给您将其转换为您需要的HTML输出。
如果有帮助,请告诉我。