我需要将一个XML String发布到一个URL端点。我熟悉这是通过JavaScript实现的,但不是通过PHP实现的。我最终将把WordPress Gravity Form变量添加到数据中,但现在并静态测试它。
我收到了cURL错误,但我对如何修复它有点不确定。
这是错误:对象引用未设置为对象的实例。
这是我的代码:
<?php
/* print the contents of a url */
function print_r_xml($arr,$wrapper = 'EstimateRequest',$cycle = 1)
{
//useful vars
$new_line = "\n";
//start building content
if($cycle == 1) { $output = 'xmlRequest='.$new_line; }
$output.= tabify($cycle - 1).'<'.$wrapper.'>'.$new_line;
foreach($arr as $key => $val)
{
if(!is_array($val))
{
$output.= tabify($cycle).'<'.htmlspecialchars($key).'>'.$val.'</'.htmlspecialchars($key).'>'.$new_line;
}
else
{
$output.= print_r_xml($val,$key,$cycle + 1).$new_line;
}
}
$output.= tabify($cycle - 1).'</'.$wrapper.'>';
//return the value
return $output;
}
/* tabify */
function tabify($num_tabs)
{
for($x = 1; $x <= $num_tabs; $x++) { $return.= "\t"; }
return $return;
}
$postStuff = array(
'ReferralCode'=> 'ELE20590',
'PrimaryContact'=>array(
'Email'=>'hello@hello.com',
'FirstName'=>'Test',
'LastName'=>'Test',
'HomePhone'=>'3035551234',
),
'PickupAddress'=>array(
'Address1'=>'410 17th St',
'City'=>'Denver',
'Zip'=>'80202'
),
'MoveDetails'=>array(
'PickupZip'=>'80202',
'DeliveryZip'=>'DeliveryZip',
'MoveDate'=>'5/1/2018',
'DwellingType'=>'10'
),
'Comments'=>'blah blah blah'
);
echo print_r_xml($postStuff);
$getPosts = print_r_xml($postStuff);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://url-of-my-endpoint",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $getPosts,
CURLOPT_HTTPHEADER => array(
"content-type: application/x-www-form-urlencoded",
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
?>
我可以通过PostMan为它提供一个XML字符串。但是,如果我知道如何克服它,那就是PHP。
这是有效的邮递员HTTP,我试图模仿(有更多的字段而不是所需的......但你明白了。
POST /sirvaxmlfeed/requestanestimate.asmx/SendEstimateRequestXmlString HTTP/1.1 Host: ballpark.allied.com Content-Type: application/x-www-form-urlencoded Cache-Control: no-cache Postman-Token: 370cade0-fafd-63fb-dd8e-25af2dfd00ab xmlRequest=
<EstimateRequest>
<ReferralCode>ELE20590</ReferralCode>
<PrimaryContact>
<Email>sample@sample.com</Email>
<FirstName>sample</FirstName>
<LastName>sample</LastName>
<PrimaryPhoneType>H</PrimaryPhoneType>
<PreferredContactTime>A</PreferredContactTime>
<HomePhone>3035551234</HomePhone>
<WorkPhone>2223334444</WorkPhone>
<WorkPhoneExt>112</WorkPhoneExt>
<CellPhone>2223334444</CellPhone>
<FaxPhone>2223334444</FaxPhone>
</PrimaryContact>
<PickupAddress>
<Address1>410 17th St</Address1>
<Address2></Address2>
<City>Denver</City>
<State>CO</State>
<Zip>80202</Zip>
</PickupAddress>
<MoveDetails>
<Process>True</Process>
<PickupZip>80202</PickupZip>
<DeliveryZip>80110</DeliveryZip>
<MoveDate>5/1/2018</MoveDate>
<DwellingType>A1</DwellingType>
<AmountOfFurnishings>M</AmountOfFurnishings>
<PickupShuttle>Y</PickupShuttle>
<DeliveryShuttle>N</DeliveryShuttle>
<AgentCode>2059000</AgentCode>
<CalendarTitle>Bob’s Appointment</CalendarTitle>
<CalendarLocation>West Wayne WASHINGTON, DC 20001 US</CalendarLocation>
<CalendarAppointmentDate>12/13/2017</CalendarAppointmentDate>
<CalendarAppointmentTime>09:00</CalendarAppointmentTime>
<CalendarAppointmentTimeOfDay>AM</CalendarAppointmentTimeOfDay>
<CalendarDuration>1 hour</CalendarDuration>
<CalendarUserName>jodoe1234@gmail.com</CalendarUserName>
<CalendarSalesPersonId>12345</CalendarSalesPersonId>
<CalendarDetail>LMP Lead ID:1026861 Cust Name:Johny Test</CalendarDetail>
<CalendarURL>
<![CDATA[http://www.google.com/calendar/feeds/jodoe1234@gmail.com/private/full/0qqbfbnjc7gr2hqa02ibua5irs]]>
</CalendarURL>
</MoveDetails>
<EstimateDetails>
<HasVehicles>Y</HasVehicles>
<RequestedEstimateDate>3D</RequestedEstimateDate>
<RequestedEstimateTimeOfDay>A</RequestedEstimateTimeOfDay>
<SpecialtyItems>sample text</SpecialtyItems>
<Comments>sample text</Comments>
<HTTPUserAgent>Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.2)</HTTPUserAgent>
<VendorId>ABC123</VendorId>
<VendorName>ABCD</VendorName>
<VendorParms>aid=12345,sid=12345</VendorParms>
<OptInForSpecialOffers>Y</OptInForSpecialOffers>
<ThirdPartyOptIn>Y</ThirdPartyOptIn>
<VendorDateTime>11/19/2015 11:30:00</VendorDateTime>
<ReferrerFirstName>Jim</ReferrerFirstName>
<ReferrerLastName>George</ReferrerLastName>
<ReferrerEmail>leadfromLMP@email.com</ReferrerEmail>
<MortgageDetails>
<Process>True</Process>
<LoanType>P</LoanType>
<LoanAmount>300000</LoanAmount>
<DownPayment>10</DownPayment>
<CurrentInterestRate>5.25</CurrentInterestRate>
<MortgageState>IL</MortgageState>
<Comments>
<![CDATA[No comments]]>
</Comments>
<CreditProfile>E</CreditProfile>
<PreQualifiedForMortgage>Y</PreQualifiedForMortgage>
<OfferCode>abc</OfferCode>
<PreferredContactTime>M</PreferredContactTime>
<ExistingHomeBalance>200000</ExistingHomeBalance>
</MortgageDetails>
<RealEstateDetails>
<Process>True</Process>
<TransactionType>X</TransactionType>
<IsHomeListedForSale>Y</IsHomeListedForSale>
<ExpirationDate>08/30/2015</ExpirationDate>
<ApproximateSalePrice>285000</ApproximateSalePrice>
<IsPreQualified>Y</IsPreQualified>
<RealEstateState>IL</RealEstateState>
<Comments>
<![CDATA[No comments even now]]>
</Comments>
</RealEstateDetails>
<HomeValue>
<Process>True</Process>
<CurrentHomeValue>285000</CurrentHomeValue>
<NewHomeValue>330000</NewHomeValue>
<AgencyNumber>AI2345</AgencyNumber>
<SalesCode>SCP0079</SalesCode>
<SalespersonName>Sales123</SalespersonName>
<SalespersonEmail>Sales@123.com</SalespersonEmail>
</HomeValue>
</EstimateDetails>
</EstimateRequest>
答案 0 :(得分:-1)
你不能这样做:
$_POST = array(
'ReferralCode'=> 'ELE20590',
'PrimaryContact'=>array(
'Email'=>'hello@hello.com',
'FirstName'=>'Test',
'LastName'=>'Test',
'HomePhone'=>'3035551234',
),
'PickupAddress'=>array(
'Address1'=>'410 17th St',
'City'=>'Denver',
'Zip'=>'80202'
),
'MoveDetails'=>array(
'PickupZip'=>'80202',
'DeliveryZip'=>'DeliveryZip',
'MoveDate'=>'5/1/2018',
'DwellingType'=>'10'
),
'Comments'=>'blah blah blah'
);
$_POST
是一个超全局,你不能将它设置为这样的变量。
如果在$cycle != 1
中定义$ output,则if
此部分将失败,因此如果未设置,则无法附加到该部分。
if($cycle == 1) {
$output = 'xmlRequest='.$new_line;
}
$output.= tabify($cycle - 1).'<'.$wrapper.'>'.$new_line;
此处存在相同问题,$return
未设置:
function tabify($num_tabs) {
for($x = 1; $x <= $num_tabs; $x++) {
$return.= "\t";
}
return $return;
}