cfhttp“POST";在php中

2022-01-09 00:00:00 php coldfusion httprequest

我有一个 ColdFusion 页面,它附加了带有表单变量的 URL.我正在尝试在 php 中复制此代码:

I have a ColdFusion page that appends the a URL with form variables. I'm trying replicate this code in php:

 <cfhttp method="Post" url='http://api.test.com/import-lead-data.php'>
            <cfoutput>
                <cfhttpparam name="BAL_ONE" type="formField" value="#BAL_ONE#">
         <!---  <cfhttpparam name="MTG_ONE_INT" type="formField" value="#MTG_ONE_INT#">
                <cfhttpparam name="MTG_TWO" type="formField" value="#MTG_TWO#">
                <cfhttpparam name="BAL_TWO" type="formField" value="#BAL_TWO#">
                <cfhttpparam name="ADD_CASH" type="formField" value="#ADD_CASH#">--->
                <cfhttpparam name="PRODUCT" type="formField" value="#PRODUCT#">
                <cfhttpparam name="PROP_ST" type="formField" value="#PROP_ST#">
                <cfhttpparam name="CRED_GRADE" type="formField" value="#CRED_GRADE#">
                <cfhttpparam name="PROP_ZIP" type="formField" value="#PROP_ZIP#">
                <cfhttpparam name="PROP_DESC" type="formField" value="#PROP_DESC#">
                <cfhttpparam name="SPEC_HOME" type="formField" value="#SPEC_HOME#">
                <cfhttpparam name="PURCHASE_CONTRACT" type="formField" value="#PURCHASE_CONTRACT#">
                <cfhttpparam name="EST_VAL" type="formField" value="#EST_VAL#">
                <cfhttpparam name="DOWN_PMT" type="formField" value="#DOWN_PMT#">
                <cfhttpparam name="LOAN_TYPE" type="formField" value="#LOAN_TYPE#">
                <cfhttpparam name="BUY_TIMEFRAME" type="formField" value="#BUY_TIMEFRAME#">
                <cfhttpparam name="AGENT_FOUND" type="formField" value="#AGENT_FOUND#">
                <cfhttpparam name="VA_STATUS" type="formField" value="#VA_STATUS#">
                <cfhttpparam name="INCOME" type="formField" value="#INCOME#">
                <cfhttpparam name="ANNUAL_VERIFIABLE_INCOME" type="formField" value="#ANNUAL_VERIFIABLE_INCOME#">
                <cfhttpparam name="FHA_BANK_FORECLOSURE" type="formField" value="#FHA_BANK_FORECLOSURE#">
                <cfhttpparam name="NUM_MORTGAGE_LATES" type="formField" value="#NUM_MORTGAGE_LATES#">
                <cfhttpparam name="EMAIL" type="formField" value="#EMAIL#">
                <cfhttpparam name="FNAME" type="formField" value="#FNAME#">
                <cfhttpparam name="LNAME" type="formField" value="#LNAME#">
                <cfhttpparam name="ADDRESS" type="formField" value="#ADDRESS#">
                <cfhttpparam name="CITY" type="formField" value="#CITY#">
                <cfhttpparam name="STATE" type="formField" value="#STATE#">
                <cfhttpparam name="ZIP" type="formField" value="#ZIP#">
                <cfhttpparam name="PRI_PHON" type="formField" value="#PRI_PHON#">
                <cfhttpparam name="SEC_PHON" type="formField" value="#SEC_PHON#">
                <cfhttpparam name="CAPTURE_METHOD" type="formField" value="#CAPTURE_METHOD#">
                <cfhttpparam name="AID" type="formField" value="#AID#">
                <cfhttpparam name="SRLP" type="formField" value="#SRLP#">
                <cfhttpparam name="SCid" type="formField" value="#SCid#">
                <cfhttpparam name="BUYER_ID" type="formField" value="#BUYER_ID#">
                <cfhttpparam name="CID" type="formField" value="#CID#">
                <cfhttpparam name="PPCID" type="formField" value="#session.PPCID#">
            </cfoutput>
        </cfhttp>

我假设我会使用 httpRquest (meth: post)?但我不确定.

I assume I would use a httpRquest (meth: post)? But I'm not sure.

推荐答案

在 PHP HTTP 是集成为所谓的流包装器­Docs.这意味着您可以将它与标准文件和流相关的功能一起使用:

In PHP HTTP is intergrated as a so called stream wrapper­Docs. That means you can use it with standard file- and stream-related functions:

$url = 'http://api.test.com/import-lead-data.php';
$result = file_get_contents($url);

这将创建一个标准的 GET 请求.但是你想要一个 POST 请求,因此你需要添加一些 上下文选项­文档:

This would create a standard GET request. But you want to have a POST request, therefore you need to add some context options­Docs:

$url = 'http://api.test.com/import-lead-data.php';
$options['http'] = array(
    'method' => "POST", 
);    
$context = stream_context_create($options);    
$result = file_get_contents($url, NULL, $context);

这将发出 POST 请求而不是 GET.仍然缺少的是表单字段.假设你有一个键 => 值对数组中的所有字段:

This would make the POST request instead of GET. What's still missing are the form fields. Let's say you have all fields in an array of key => value pairs:

$url = 'http://api.test.com/import-lead-data.php';
$content = http_build_query($form_fields);    
$options['http'] = array(
    'method' => "POST", 
    'content' => $content,
);    
$context = stream_context_create($options);    
$result = file_get_contents($url, NULL, $context);

希望对您有所帮助.当然,您也可以使用 http_post_datahttp_post_fields 函数(如果有的话).

Hope that's helpful. Naturally you can use as well the http_post_data or http_post_fields function as well if you have it available.

参见:HEAD first with PHP Streams.

相关文章