PHP cURL with SS validation -
i have working curl scripts have been relying on remote server ss validation. want add validation our curl scripts if aren't filled out correctly request won't sent. have client side js validation, want duplicate ss validation. here example of curl script:
<?php $url = 'https://remoteserver.com/post.svc/foo'; $field1 = $_post["field1"]; //other input data $fields = array( 'field1'=>urlencode($field1), //other input data ); foreach($fields $key=>$value) { $fields_string .= $key.'='.$value.'&'; } $fields_string = rtrim($fields_string,'& '); $ch = curl_init($url); curl_setopt($ch, curlopt_header, 0); curl_setopt($ch, curlopt_post, 1); curl_setopt($ch, curlopt_postfields, $fields_string); curl_setopt($ch, curlopt_ssl_verifyhost, 0); curl_setopt($ch, curlopt_ssl_verifypeer, 0); curl_setopt($ch, curlopt_returntransfer, 1); $output = curl_exec($ch); echo $output; curl_close($ch); ?> can create if/else statement like:
<?php if(empty($_post['field1']))) { echo "error"; die(); } else { $url = 'https://remoteserver.com/post.svc/foo'; $field1 = $_post["field1"]; //other input data $fields = array( 'field1'=>urlencode($field1), //other input data ); foreach($fields $key=>$value) { $fields_string .= $key.'='.$value.'&'; } $fields_string = rtrim($fields_string,'& '); $ch = curl_init($url); curl_setopt($ch, curlopt_header, 0); curl_setopt($ch, curlopt_post, 1); curl_setopt($ch, curlopt_postfields, $fields_string); curl_setopt($ch, curlopt_ssl_verifyhost, 0); curl_setopt($ch, curlopt_ssl_verifypeer, 0); curl_setopt($ch, curlopt_returntransfer, 1); $output = curl_exec($ch); echo $output; curl_close($ch); } i have ajax stuff displayed if return "error". can't easy, it?
thx
instead of returning 'error', why not return useful stuff? php structure such as
$request_status = array( 'error' => 1, 'error_msg' => 'form not completed properly' 'error_details' => array( 0 => 'name field not completed', 1 => 'invalid state specified', 2 => 'password1 , password 2 not match' ) ) echo json_encode($request_status); would of far more use. client-side ajax code can 'error' parameter in there , either go "hey, worked!" or further based on error_details supplied, such highlighting form fields weren't completed correctly.
this allows send other types of error messages well, such saying "hey, place we're curling form isn't responding" , like.
Comments
Post a Comment