0

Option 1:

$data= array( "Code" => "abcde", "Id" => "A007", "RefNo" => "123456", "UserName" => "QWE", "UserEmail" => "[email protected]", ); $url="https://testing.php"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $result=curl_exec($ch); curl_close ($ch); echo $result; } 


Option 2:

<form method="post" action="https://testing.php"> <input type="hidden" value="abcde" name="Code"> <input type="hidden" value="A007" name="Id"> <input type="hidden" value="QWE" name="UserName"> <input type="hidden" value="[email protected]" name="UserEmail"> <input type="hidden" value="123456" name="RefNo"> <input type="submit" name="submit"> </form> 


Is there any different between A and B? Because i tried both but the curl only get the "fail" response from the api.

4
  • what is the error from the curl request? the http_build_query() will convert your post data into a query string, is that the behaviour you want? Commented Oct 1, 2018 at 8:49
  • Maybe your API needs some specific header ? Commented Oct 1, 2018 at 8:50
  • @noid i new to curl, is the query string different with the post data in option b? Commented Oct 1, 2018 at 9:54
  • @MounirOnGithub dont think so, they didnt say anything about it Commented Oct 1, 2018 at 9:55

2 Answers 2

5

There is no difference since they both sends POST request, but you can say the only difference in the technique you are using:

  • the first one can be done completely from back-end and it will allow you to validate your data before sending it to the API.
  • Second will not allow such thing, you may have to write javascript code to validate before submitting.

The error you will be facing might be due to a missing data in your request. or maybe your IP address is not white-listed on the server you are trying to access its API.

Also, there is no such URL, https://testing.php, try to use your IP address or full server address to send the request.

Sign up to request clarification or add additional context in comments.

3 Comments

FWIW, it might be good to explain that you can prevent JS to run thereby preventing any potential client side validation and leaving your data to be modified with no potential checks in place.
Sorry, the URL is fake. I tried send the same request to my other domain and all of them get the data. option a and b are using same url. Thx for the explanation, i'll try it again
i think i found the problem, i get different ip address from the option a and b. Can i change the ip in curl?
2

You can use below code:

$url = 'testing.php'; $fields = array( 'Id' => urlencode($_POST['Id']), 'Code' => urlencode($_POST['Code']), 'UserName' => urlencode($_POST['UserName']), 'UserEmail' => urlencode($_POST['UserEmail']), 'RefNo' => urlencode($_POST['RefNo']) ); 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); 

Execute post:

$result = curl_exec($ch); 

Close connection:

curl_close($ch); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.