I select field values for a row WHERE the httpCode is <> 200, then post these values to a remote url with PHP cURL. I then UPDATE the httpCode for this row with the new response from the remote url.
The problem I am experiencing is that the remote logs are showing that all the rows are being transmitted in one line instead of transmitting one line per row. For example if I have 3 rows where the httpCode = 0, the 3 rows will post and insert correctly into the remote database, but the remote log will show the data for all three rows in one entry. Here is a log example:
POST /web HTTP/1.1 User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1) Host: 255.255.255.255 Accept: */* Content-Length: 3291 Content-Type: application/x-www-form-urlencoded Expect: 100-continue VendorID=web&Type=web&FirstName=Test1&LastName=Test1&VendorID=web&Type=web&FirstName=Test2&LastName=Test2&VendorID=web&Type=web&FirstName=Test3&LastName=Test3
This is how the log should look:
POST /web HTTP/1.1 User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1) Host: 255.255.255.255 Accept: */* Content-Length: 3291 Content-Type: application/x-www-form-urlencoded Expect: 100-continue VendorID=web&Type=web&FirstName=Test1&LastName=Test1
POST /web HTTP/1.1 User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1) Host: 255.255.255.255 Accept: */* Content-Length: 3291 Content-Type: application/x-www-form-urlencoded Expect: 100-continue VendorID=web&Type=web&FirstName=Test2&LastName=Test2
POST /web HTTP/1.1 User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1) Host: 255.255.255.255 Accept: */* Content-Length: 3291 Content-Type: application/x-www-form-urlencoded Expect: 100-continue VendorID=web&Type=web&FirstName=Test3&LastName=Test3
The remote database appears to insert each row correctly, only the log combines the rows into a single entry.
My code for the script is below. I appreciate any help!
<?php
// Connects to Database
mysql_connect("localhost", "username", "password") or die(mysql_error());
// select database.
mysql_select_db("dbname") or die(mysql_error());
// Select data from mysql
$rows = mysql_query(
"SELECT
id,
VendorID,
Type,
FirstName,
LastName
FROM tablename
WHERE
httpCode <> 200"
);
while($row = mysql_fetch_array($rows)) {
// Fill CURL POST fields with this information,
//set POST variables
$url = 'https://domain.com';
$fields = array(
'VendorID' => urlencode($row['VendorID']),
'Type' => urlencode($row['Type']),
'FirstName' => urlencode($row['FirstName']),
'LastName' => urlencode($row['LastName'])
);
//format the data for the url POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
//execute post
$result = curl_exec($ch);
// Get values from response header
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
//close connection
curl_close($ch);
// Update httpCode in mysql
mysql_query("UPDATE tablename SET httpCode='$httpCode' WHERE id='".$row['id']."'" )
or die(mysql_error());
} // repeat loop to attempt a POST on the next record...
?>
Remote log is combining rows in PHP cURL post
Moderator: General Moderators
Re: Remote log is combining rows in PHP cURL post
the log is doing its job correctly.
you have no reset for the $fields_string and it thus concantenate perpetually .
each access then passes the post from previous added on and thus you will have one hell of a long post eventually
you need
$fields_string = '';
before
//format the data for the url POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');
you have no reset for the $fields_string and it thus concantenate perpetually .
each access then passes the post from previous added on and thus you will have one hell of a long post eventually
you need
$fields_string = '';
before
//format the data for the url POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');