Link to home
Start Free TrialLog in
Avatar of razn8
razn8

asked on

cURL PHP form post with receipt verification and timeout repost

I must insert form data to my local mysql table, then post the form data to a URL string for the client's a mssql server. In the event of a time out, I need to retrieve a response from the remote server to verify the post has been received. If a response is received, the record in my local mysql table is flagged as delivered, If no response is received, the record data is reposted to the url until a response is received.

I have successfully tested the url post using cURL. What I need help with is retrieving the response, then flagging the record as delivered or reposting the record.

This is the final url format I will need to post.

https://255.255.255.0/WebTransfer?VendorID=userName&Type=newRecord&formfield1=value&formfield2=value

This is my test url; http://255.255.255.0/WebTransfer/, which is an index.php file that receives the url post and inserts the data into a second mysql table to simulate the client's server.

My code is attached.

<?php
    // Connects to Database
    mysql_connect("localhost", "username", "password") or die(mysql_error()); 
	// select database.
	mysql_select_db("dbname") or die(mysql_error());
	
	// Get values from form
	$VendorID = 'username';
	$Type= 'new_record';
	$firstName = $_POST['firstName'];
	$lastName= $_POST['lastName'];
	
	
	// Insert data into mysql
    mysql_query("INSERT INTO names VALUES ( '$VendorID', '$Type', '$firstName', '$lastName'  )");
 
	
// send to url
 
 
//extract data from the post
extract($_POST);
 
//set POST variables
$url = 'http://255.255.255.0/WebTransfer/';
$fields = array(
                      'VendorID'=>urlencode($VendorID),
					 'Type'=>urlencode($Type),
					  'firstName'=>urlencode($firstName),
                      'lastName'=>urlencode($lastName)
               );
 
//url-ify the data for the 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);
 
//close connection
curl_close($ch);
	
    ?>

Open in new window

SOLUTION
Avatar of agamal
agamal
Flag of United Arab Emirates image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of razn8
razn8

ASKER

I may not have clearly asked this question. Let me restate the question:

I'm inserting form data to my local mysql table, then I post the form data via cURL to a client's remote url.

I need help with recording a response into my local MySQL table that the client's remote server has received the cURL form post. If a response is not received from the client's remote server, I need to resend the form data post every 10 minutes until a response is recieved. Once a response is received I need to record it in my local MySQL table entry.

So I think I need to figure out the following:

How do I capture and insert the response receipt into the corresponding MySQL table entry?
How do I check for the response record every ten minutes and repost the form data if no response is recieved?
may i know how you will receive response ... on page (HTML) or direct connection to mysql remotely on 3306 ???
Avatar of razn8

ASKER

That would be the first step that I need advice on.

If the url post is received immediately, I asume an on page receipt and subsequent MySQL update would work. But if the url post times out, the MySQL table entry needs to checked for a receipt every xx minutes until a response is received. Once a response is received the mysql entry is updated as response received and the entry no longer needs to be checked .

I am guessing that, along with my form data fields, I need to create a Received field. If a remote response from the url post is received, the Received field is updated to Yes, and the entry is complete. If there is no response from the remote server, the Received field is updated to No. If the Received field value is No, the data for that entry is reposted to the remote url. If a response from the repost is received, the Received field is updated to Yes, and the entry is complete. If the repost does not generate a response from the remote server, the Received field is not updated and this process will repeat in ten minutes, or whatever time delay I specify.

What do you recommend? Is my explanation clear?
what i tried to tell is ... if the form url is
http://www.example.com/form.php

and the response url is
http://www.example.com/result.php

that means if i entered the data to form.php the successful page will be result.php

then you can cURL form.php and post/get the variable you like .... make your script wait for 10 seconds then curl result.php and search in its content for the success message

if not make the script which chesk for success to be refreshed every 10 min or whatever you like using HTML refresh or header refresh
Avatar of razn8

ASKER

It seems that what I need to do is figure out how to set curl to return the response headers, use curl functions to read them and mark the status in my mysql table record as Successful or Unsuccessful.

Then I need to figure out how to create a crontab that reads the mysql record status and reposts the form data to the url if it is marked as Unsuccessful.

I used curl_getinfo() function and curl_errno() function to print the following:

Array ( [url] => http://www.domain.com/ [content_type] => text/html [http_code] => 200 [header_size] => 159 [request_size] => 264 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 0.057687 [namelookup_time] => 0.016364 [connect_time] => 0.016512 [pretransfer_time] => 0.016519 [size_upload] => 63 [size_download] => 1246 [speed_download] => 21599 [speed_upload] => 1092 [download_content_length] => 1246 [upload_content_length] => 0 [starttransfer_time] => 0.057619 [redirect_time] => 0 ) 0

Can I use this to read the url post response? How would I translate it and insert the Successful record into the mysql table?
Avatar of razn8

ASKER

agamal,

I believe I should delete this question and break it into separate questions
Avatar of razn8

ASKER

agamal,

Instead of deleting this question, I will just be more specific to one part of the question.

Once I've queried an unsuccessful row and repost those values to the remote url, how would I update that row's http response code field with the response code from the repost?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial