如何使用PHP和cURL提交POST数据? [英] How do I submit POST data using PHP and cURL?

查看:202
本文介绍了如何使用PHP和cURL提交POST数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以帮助我如何使用PHP和cURL将数据从表单(POST)发送到URL?

Can anyone help on how can I do to send the data from a form (POST) to an URL using PHP and cURL?

使用cURL,所有我想要完成的是发送数据,我不想被重定向也不得到任何类型的输出(HTML或TEXT)数据。

Using cURL, all I want to accomplish is send the data, I don't want to be redirected nor get any sort of output (HTML nor TEXT) just submit the data.

很高兴知道数据是否已成功提交或无法处理错误或重定向。

It would be nice to know if the data has been submitted successfully or not to handle error or redirection.

其他信息。我认为我被重定向的原因是,一旦我执行cURL,目标页面,这是一个第三方网站,有一个重定向到位,确认用户他们的数据已经收到,以及由于某种原因,当我发送我的数据使用cURL他们的重定向它影响我的页面,因此我的页面也重定向到他们的确认网站。

Additional Info. The reason I think I'm getting redirected is because once I execute the cURL, the destination page, which is a third party website, have a redirect into place confirming the user that their data has been received, and for some reason, when I send my data using cURL their redirection it effects my page, therefore my page it also redirects to their confirmation site.

谢谢大家。

示例代码:

$sub_req_url ="http://domain.com/util/request";
$ch = curl_init($sub_req_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS,  "id=64&session=1&k=B0EA8835E&firstname=Name&lastname=Lastname&company=Company%20Name&email=me@gmail.com&work_phone=123-456-7890");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);

$resp = curl_exec($ch);
curl_close($ch);

我编辑此帖子以显示我正在使用的示例。

I edit this post to show an example of what I'm using. I should posted the code in first place.

推荐答案

function post($requestJson) {

    $postUrl = $this->endpoint."?access_token=".$this->token;

    //Get length of post
    $postlength = strlen($requestJson);

    //open connection
    $ch = curl_init();

    //set the url, number of POST vars, POST data
    curl_setopt($ch,CURLOPT_URL,$postUrl);
    curl_setopt($ch,CURLOPT_POST,$postlength);
    curl_setopt($ch,CURLOPT_POSTFIELDS,$requestJson);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $response = curl_exec($ch);

    //close connection
    curl_close($ch);

    return $response;
}

这篇关于如何使用PHP和cURL提交POST数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆