我在php中有一个foreach语句,它为每个条目从数据库加载一些数据并将其发送到特定地址:
foreach ($affiliates['response']['data'] as $aff_id => $data) {
$curl_connection =
curl_init('https://crm.zoho.com/crm/WebToLeadForm');
//set options
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl_connection, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);
...
//traverse array and prepare data for posting (key1=value1)
foreach ($post_data as $key => $value) {
$post_items[] = $key . '=' . $value;
}
//create the final string to be posted using implode()
$post_string = implode('&', $post_items);
//set data to be posted
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);
//perform our request
$result = curl_exec($curl_connection);
//show information regarding the request
echo '<pre>';
print_r(curl_getinfo($curl_connection));
echo curl_errno($curl_connection) . '-' .
curl_error($curl_connection);
echo '</pre>';
//close the connection
curl_close($curl_connection);
}
结果是第一个条目正确,但缺少以下条目。 我打印出curl-messages并意识到我在curl_init中输入的链接与第二次尝试不同。
首先看起来像:
然后将其编辑为
我是否必须设置一些其他选项,或者我是否设置了其中一些选项?
答案 0 :(得分:1)
<强>选项1:强>
尝试在for循环开始时取消设置$ curl_connection
前两行将成为:
foreach ($affiliates['response']['data'] as $aff_id => $data) {
unset( $curl_connection );
$curl_connection = curl_init('https://crm.zoho.com/crm/WebToLeadForm');
...
选项2 因为你连接到curl_init中的同一个url,所以你可以在循环之前连接。即。
$curl_connection = curl_init('https://crm.zoho.com/crm/WebToLeadForm');
foreach ($affiliates['response']['data'] as $aff_id => $data) {
...