通过curl将数据发布到api

时间:2018-09-24 10:07:50

标签: php redirect curl

我尝试将一些数据发布到api,但是我对此有疑问(该页面仅在我提交请求后才加载)
我的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);


期望:
提交请求后,API会将页面从request.php重定向到response.php。
P.S:因英语不好而卷曲和ry吟的新手

1 个答案:

答案 0 :(得分:3)

远程API不会重定向您的客户端,但是您应该这样做:

$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();
}