[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

cURL PHP form post with receipt verification and timeout repost

Posted on 2009-02-15
9
Medium Priority
?
932 Views
Last Modified: 2013-12-12
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

0
Comment
Question by:razn8
  • 6
  • 3
9 Comments
 
LVL 8

Assisted Solution

by:agamal
agamal earned 200 total points
ID: 23647520
Link is not working ....

Anyways

What you need to do is  making  a loop to insert your code ... then make another cURL to retrive the response page if it is containing the successful reply then break the loop if not retry the loop (RE INSERT THE CODE)
0
 

Author Comment

by:razn8
ID: 23648964
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?
0
 
LVL 8

Expert Comment

by:agamal
ID: 23649000
may i know how you will receive response ... on page (HTML) or direct connection to mysql remotely on 3306 ???
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:razn8
ID: 23649558
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?
0
 
LVL 8

Expert Comment

by:agamal
ID: 23650370
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
0
 

Author Comment

by:razn8
ID: 23651516
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?
0
 

Author Comment

by:razn8
ID: 23652187
agamal,

I believe I should delete this question and break it into separate questions
0
 

Author Comment

by:razn8
ID: 23687721
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?
0
 

Accepted Solution

by:
razn8 earned 0 total points
ID: 23701162
I think I figured it out:

<?php
    // Connects to your Database
    mysql_connect("localhost", "user", "password") or die(mysql_error());
      // select database.
      mysql_select_db("dbname") or die(mysql_error());
      
            // Select data from mysql
      $rows = mysql_query(
      "SELECT
            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 = 'http://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
      $row = mysql_query("UPDATE tablename SET httpCode='$httpCode'" )
      or die(mysql_error());

} // return to loop to attempt a POST on the next row with an unsuccessful response code ...
      
    ?>

Thanks for the help anyways
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses
Course of the Month18 days, 14 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question