我使用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暂时移动
在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 ),但是如果我把它放在URL中,我会得到302。
解决了问题-- $remoteArray确实有一个错误。最初,$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中正常工作。感谢那些回应的人!
发布于 2014-06-24 17:05:54
这不是一个正式的答案,只是OP引用的一个结构示例,因为PHP跨越了:
fieldType可以是url或formfield
方法可以是get或post。
在密码示例中,我已经创作了,所以我只是传递令牌,这样我就可以得到一些东西。
您的argscollection是一个可以循环使用的structure...so。
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,因为有可能已经构建了一个Cold聚变包装器,并在那里等待您。
发布于 2014-06-26 03:32:59
$remoteArray确实有一个错误。最初,$apiauthkey、$apicomponent和$apimethod是在数组之外定义的。一旦它们被添加到用于json编码的数组中,它就可以工作了。
https://stackoverflow.com/questions/24389735
复制相似问题