2012-09-27 50 views
2

我需要通过一个字符串后从CSV提交一组数据到一个网址,要做到这一点我做了下面的循环和卷曲,垃圾:虽然在提交网址

<?php 

$CSVleadfile = "leads.csv"; 

//lead file information lookup 

$f = fopen($CSVleadfile, "r"); 

//begin while loop 

while($row = fgetcsv($f)) { 
     $contactId = $row[1]; 
     $createDate = $row[2]; 
     $callerAni = $row[3]; 
     $tfn = $row[4]; 
     $firstname = $row[5]; 
     $lastname = $row[6]; 
     $address1 = $row[7]; 
     $address2 = $row[8]; 
     $city = $row[9]; 
     $state = $row[10]; 
     $zip = $row[11]; 
     $homephone = $row[12]; 
     $email = $row[13];  

// load data for posting array 

$post_data = array(
"Prs_First" => "$firstname", 
"Prs_Middle" => "", 
"Prs_Last" => "$lastname", 
"Prs_Email" => "$email", 
"Prs_Phone1" => "$homephone", 
"Prs_Phone2" => "$callerAni", 
"Prs_Address1" => "$address1", 
"Prs_Address2" => "$address2", 
"Prs_City" => "$city", 
"Prs_State" => "$state", 
"Prs_Zip" => "$zip", 
"InitialContactDate" => "$createDate", 
); 

//traverse array and prepare data for posting (key1=value1) 

unset($post_items); 

foreach ($post_data as $key => $value) { 
    $post_items[] = $key . '=' . $value; 
} 

//create the string to be posted using implode() 

$post_string = implode ('&', $post_items); 

//create cURL connection 

$curl_connection = curl_init('https://www.whatever.com'); 

//echo to make sure string is formed correctly 

echo "Submitting: <br />"; 
echo "$post_string"; 
echo "<br />"; 


//set options 

curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30); 
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1); 

//set data to be posted 

curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string); 

//perform the request 

$result = curl_exec($curl_connection); 

$result = simplexml_load_string($result); 
$result = json_encode($result); 
$result = json_decode($result, true); 

echo "<br />"; 
if (isset($result)){ 
echo "<pre>"; 
print_r($result); 
echo "$result"; 
print_r($result[0]); 
echo "$result[0]"; 
echo "</pre>"; 
} else { 
echo "it's still broken."; 
$result = "Still TFB"; 
} 
echo "<br />"; 

unset($curl_connection); 
unset($result); 
unset($post_string); 

} 

//end while loop 

fclose($f); 

?> 

现在,我遇到的问题有两个方面:首先,我从来没有从最后的回显中看到任何结果,无论我尝试echo $ result还是print_r。第二个问题是这需要FOREVER。我们正在谈论30秒的提交......这让我怀疑是否由于某种原因超时?我从来没有像以前那样在一个while循环中使用curl,所以我不确定是否需要进行某种附加清理......或者以某种方式回显更多信息,以便从curl进行故障排除。我开始觉得自己在我的智慧结束了......哈尔普!

编辑:

好了,我已经扔卷曲外循环功能的建议:

<?php 

$CSVleadfile = "testing.csv"; 

//lead file information lookup 

$f = fopen($CSVleadfile, "r"); 

while ($row = fgetcsv($f)) { 
$contactId = $row[1]; 
$createDate = $row[2]; 
$callerAni = $row[3]; 
$tfn = $row[4]; 
$firstname = $row[5]; 
$lastname = $row[6]; 
$address1 = $row[7]; 
$address2 = $row[8]; 
$city = $row[9]; 
$state = $row[10]; 
$zip = $row[11]; 
$homephone = $row[12]; 
$email = $row[13]; 

// load data for posting array 
$post_data = array(
    "that" => "$junk", 
); 

//traverse array and prepare data for posting (key1=value1) 
foreach ($post_data as $key => $value) { 
    $post_items[] = $key . '=' . $value; 
} 

//create the string to be posted using implode() 
$post_string = implode('&', $post_items); 


//perform the request 
executeCurl(); 

} 

//outside loop curl function 

function executeCurl() { 

//create cURL connection 
$curl_connection =  curl_init('https://www.whatever.com'); 

//set options 

curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30); 
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1); 

//set data to be posted 

curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string); 


$result = curl_exec($curl_connection); 

$result = simplexml_load_string($result); 

echo "<br />"; 
if (isset($result)) { 
    var_dump($result); 
    die; 
} else { 
    echo "it's still broken."; 
    $result = "Still TFB"; 
} 
echo "<br />"; 

file_put_contents("./Results.txt", $results, FILE_APPEND | LOCK_EX); 
} 

fclose($f); 
?> 

我已经剥离了JSON古怪离开那里......说实话着记得我为什么扔在那里,肯定它可能已经格式化但无论哪种方式,我期待的结果是一样的东西:

<string xmlns="http://www.whatever.com">11940576</string> 

,这就是我想要记录回该文本文件:D没有改变行为,虽然:(我var_dump结果在布尔(假)

+1

例如,如果可以的话,最好重用连接,即将'curl_init()'移到循环外部。另外,simplexml_load_string() - > json_encode() - > json_decode()代码是怎么回事?你期望得到什么? –

+1

#1,我会使用file_get_contents()而不是fopen/fclose/etc。这应该有助于降低一些复杂性。 #2,你期待从cURL POST得到什么回应? #3,试试'var_dump($ result);死亡;'确切地看到cURL请求中吐出的是什么。 –

+1

@ c.hill'file_get_contents()'不能很好地与'fgetcsv()'配合使用;也许你可以使用'str_getcsv()',但这不会提高内存效率。 –

回答

0

问题1:为什么你将你的发布数据数组转换为字符串? Curl愉快地接受CURLOPT_POSTFIELDS的数组,并且使用一个数组,另外将Content-Type头部设置为multipart/form-data。

问题2:curl将同时接受curl_setopt_array()的多个选项。