2

我需要通过帖子字符串将一堆数据从 CSV 提交到 url,为此我正在执行以下操作:

<?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);

?>

现在,我遇到的问题有两个方面:首先,无论我尝试回显 $result 还是 print_r,我都看不到最后回显的任何结果。第二个问题是这需要永远。我们正在谈论 30 秒的提交......这让我想知道它是否由于某种原因超时?我以前从来没有在这样的while循环中使用过curl,所以我不确定是否需要做一些额外的清理......或者以某种方式回显更多信息以从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 导致 bool(false)

4

1 回答 1

0

问题一:你为什么要把你的帖子数据数组转换成字符串?Curl 很高兴地接受了一个数组CURLOPT_POSTFIELDS,并使用一个数组将 Content-Type 标头设置为 multipart/form-data。

问题 2: curl 将使用curl_setopt_array().

于 2012-10-03T17:08:55.347 回答