简体   繁体   English

PHP cURL 后数据未发送到目标 url

[英]PHP cURL post data is not sent to target url

Maybe it's duplicate question.也许这是重复的问题。 I have tried any solution with similar question with mine that I can find and implemented it to my probs, not a single solution is work.我已经尝试过任何与我有类似问题的解决方案,我可以找到并将其实施到我的问题上,没有一个解决方案是可行的。 I need to use PHP curl to get data from other systems (B).我需要使用 PHP curl 从其他系统(B)获取数据。 But in the B the data I sent is not defined or no data was sent.但是在 B 中,我发送的数据未定义或未发送任何数据。 curl process is success target url is just right. curl 进程是成功目标 url 正好。

Curl in my system Curl 在我的系统中

$data['tag']    = 'getData';
$data['periode'] = $this->input->post('periode');
$url = 'http://example.com/service/index.php';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, sizeof($data));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// I use follow location cause I get moved permanently notice
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$output = curl_exec($ch);
curl_close($ch);

echo $output;

And in B system I check with this在 B 系统中,我检查了这个

var_dump($_POST);

the ouput is array(0) {} .输出是array(0) {}

You need to use CURLOPT_POSTFIELDS in a query string format.您需要以查询字符串格式使用 CURLOPT_POSTFIELDS。

curl_setopt($ch, CURLOPT_POSTFIELDS, "tag=val&periode=val");

Also you should just be setting CURLOPT_POST to 1 as it just specifies if it should use a POST request.此外,您应该将 CURLOPT_POST 设置为 1,因为它只是指定它是否应该使用 POST 请求。

You can use https://www.php.net/manual/en/function.http-build-query.php to build a query string from an array.您可以使用https://www.php.net/manual/en/function.http-build-query.php从数组构建查询字符串。

I have my own solution for this.我有我自己的解决方案。 I just need to set the URL to https insetad of http , because the backend program is redirecting all the non secure protocol http to the secure one https . I just need to set the URL to https insetad of http , because the backend program is redirecting all the non secure protocol http to the secure one https .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM