我有一个使用Coldfusion和cfhttp的API的工作调用,如下所示:
<cfhttp url="http://api.domain.com/api/proxy.cfc" method="post" result="httpResult" charset="UTF-8">
<cfhttpparam type="url" name="method" value="apiauth"/>
<cfhttpparam type="url" name="argumentCollection" value="#jsData#"/>
</cfhttp>
#jsData#是一个json字符串,表示一个数据数组。
我们之间有什么问题
<cfhttpparam type="url" name="method" value="apiauth"/>
使用cURL。如果我在URL后面加上
http://api.domain.com/api/proxy.cfc?method=apiauth
我得到一个响应:302 Moved Temporarily
在PHP中,我已经创建了我的数组$remoteArray(数据工作正常,所以问题不存在),我已经尝试过这个作为我的CURL:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'http://api.domain.com/api/proxy.cfc',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => array(
'method' => 'apiauth',
'argumentCollection' => json_encode($remoteArray)
)
));
$resp = curl_exec($curl);
,但它不工作(可能是因为method=>apiauth不是post字段-它是一个URL参数-但我得到一个302,如果我把它放在URL。
SOLVED - $remoteArray did有错误。最初,$apiauthkey、$apicomponent和$apimethod是在数组之外定义的。一旦它们被添加到json编码数组中,它就工作了。这里是$remoteArray创建的地方:
$remoteArray = array(
"apiauthkey" => "$apiauthkey",
"apicomponent" => "$apicomponent",
"apimethod" => "$apimethod",
"apiarguments" => array(
'address_1'=>"test 1",
'address_2'=>"test 2",
'city'=>"new york",
'email'=>"user@domain.com",
'first_name'=>"test fname",
'last_name'=>"test lname",
'ph_num'=>"2155551212",
'state'=>"NY",
'zip'=>"90210",
'rbpid' => $rbpid,
)
);
一旦正确设置了嵌套数组,它就可以很好地使用cURL。感谢那些回应的人!
不是一个正式的答案,只是一个结构的例子供OP参考,因为PHP交叉:
fieldType可以是url
或formfield
方法可以是get
或post
在密码的例子中,我已经授权了,所以我只是传递令牌,所以我可以得到一些东西。
你的argscollection
是一个结构…所以你可以在上面循环。
requestOject
是一个正常的结构,常见的期望的东西在cfhhtp调用名称和匹配的argsCollection
值(然后是参数的类型)
<cfhttp url="#url#"
method="#method#"
result="response"
username="#AccountID#"
password="#AuthToken#">
<cfloop collection="#argscollection#" item="v">
<cfhttpparam name="#parameterTarget(v, requestObject)#"
value="#argscollection[v]#"
type="#fieldType#" />
</cfloop>
</cfhttp>
<cfdump var="#response#">
我相信这会让你的头脑中产生更多的"可能性"。或者激发其他CFHTTP专家加入进来…
此外,如果这是一个常见的库,你是从拉/交谈,然后考虑RIAForge, CFLib或GitHub,因为机会是有一个Coldfusion包装已经构建和等待你在那里。
$remoteArray确实有错误。最初,$apiauthkey、$apicomponent和$apimethod是在数组之外定义的。一旦它们被添加到json编码数组中,它就工作了。