1

I have this script ProtectedLinks from CodeCanyon. It make temporary links using form inputs. I needed it's functionality. so I decided to use CURL. First, Did I make the right decision?

In my project, I have to send for example 30 CURL request (direct links and other $_POST data needed by the page which takes these data and convert them to temporary links and store it in a database) to this page. It's a heavy work.

It sometimes make my page stop and insert half data which is very unpleasant. What should I do?

Is there a way I send one request, with one array consisting all data, while doing the same thing.

This is my data:

$fields = array
(  
'url' => $url_to_be_coded,  
'f_name' => "Package " . $product_id,  
'f_title' => "Package " . $product_id,  
'f_desc' => "Part" . $part,  
'usertype' => 'm',  
'exptime' => '72',  
'exprespect' => 'L',  
'authority' => "User Requested Again",  
'part_number' => $part  
);  

I want to send a lot of this, in one request or call. with the same keys where number of items in array is not clear. and of course process this data in landing page

Messi Meysam
  • 201
  • 1
  • 9
  • If you're using pdo, you can check this out for the data insertion. http://php.net/manual/en/pdo.begintransaction.php If not, check this out which should be the first option https://dev.mysql.com/doc/refman/5.7/en/commit.html – Stark Dec 16 '17 at 08:20
  • @Johnson Unfortunately I'm using MYSQLI – Messi Meysam Dec 16 '17 at 08:22
  • Here then, http://php.net/manual/en/mysqli.begin-transaction.php and in case you get lost https://stackoverflow.com/questions/12091971/how-to-start-and-end-transaction-in-mysqli – Stark Dec 16 '17 at 08:23

1 Answers1

2

Did I make the right decision? - yup, sounds good to me.

It sometimes make my page stop and insert half data which is very unpleasant. What should I do? what the f? as @Johnson said in the comments, wrap it around a transaction so it becomes an everything or nothing deal, but also try to find out why this sometimes happen, which it shouldn't.. php max execution time? nginx proxy_read_timeout? php ignore_user_abort()? curl CURLOPT_TIMEOUT? whatever it is, track it down, and fix it if possible. does it generate anything in the error logs?

I want to send a lot of this, in one request or call. with the same keys where number of items in array is not clear. and of course process this data in landing page - no problem, instead of accepting a single array of your fields, change the code to accept an array of arrays of fields, and wrap your current field handling code in a foreach($_POST as $field){/*process single field just like in the old code*/}

  • but if it's a lot of data, and you're not uploading it to a local LAN but over a slower net, you might wanna compress it before sending it, eg, on the client do

$fields=gzcompress(http_build_query($fields),9); and CURLOPT_HTTPHEADER=>['Content-Type: application/x-www-urlencoded','Content-Encoding: gzip'];

and on the server do

if($_SERVER['HTTP_CONTENT_ENCODING']==='gzip'){
    parse_str(gzuncompress(file_get_contents('php://input')),$_POST);
}
hanshenrik
  • 19,904
  • 4
  • 43
  • 89