问题
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>
I assume I would use a httpRquest (meth: post)? But I'm not sure.
回答1:
In PHP HTTP is intergrated as a so called stream wrapperDocs. 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);
This would create a standard GET request. But you want to have a POST request, therefore you need to add some context optionsDocs:
$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);
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);
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.
See as well: HEAD first with PHP Streams.
回答2:
The HTTP PECL extension (which contains the HTTPRequest class) isn't part of a default PHP installation.
I would take a look at CURL: http://php.net/manual/en/book.curl.php. You can set the HTTP method and parameters.
Or, if you don't mind using a framework, checkout the Zend FW HTTP Client module: http://framework.zend.com/manual/en/zend.http.html
Here's an example of using CURL to do a post request: http://davidwalsh.name/execute-http-post-php-curl
来源:https://stackoverflow.com/questions/9703879/a-cfhttp-post-in-php