0

I trying to post some data to an api but i having problem on that(the page only loading after i submit the request)
My request.php

$ch = curl_init()
curl_setopt($ch,CURLOPT_URL,"https://example/epayment/xxx.asp");
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELD,json_encode($data));
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);   
curl_setopt($ch,CURLOPT_AUTOREFERER,1);
curl_setopt($ch,CURLOPT_HTTPHEADER, array(                                                                                      
    'Content-Type: application/json',                                                                                            
    'Content-Length:'.strlen(json_encode($data)))                                                                                   
 );
$return=curl_exec($ch);
echo $return;
curl_close($ch);


Expectation:
After submit the request, the api will redirect the page from request.php to response.php .
P.S :New to curl and sry for bad english

2
  • 1
    Curl itself can redirect, but if you want to redirect the request.php file use header('Location: '.$newURL); Commented Sep 24, 2018 at 10:10
  • 1
    Incidentally it ought to be CURLOPT_POSTFIELDS rather than CURLOPT_POSTFIELD I believe. Is response.php supposed to process the actual response in some way? Commented Sep 24, 2018 at 10:12

1 Answer 1

3

The remote API won't redirect your client, but you should do it:

$ch = curl_init()
curl_setopt($ch,CURLOPT_URL,"https://example/epayment/xxx.asp");
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELD,json_encode($data));
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);   
curl_setopt($ch,CURLOPT_AUTOREFERER,1);
curl_setopt($ch,CURLOPT_HTTPHEADER, array(                                                                                      
    'Content-Type: application/json',                                                                                            
    'Content-Length:'.strlen(json_encode($data)))                                                                                   
 );
$return=curl_exec($ch);
$err = curl_error($ch);
//echo $return;
curl_close($ch);
if ($err) {
    echo $err;   
} else {
    header('Location: response.php');
    exit();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.