简体   繁体   English

如何使用 POST 通过 curl 发送 WordPress 表单数据?

[英]How to send WordPress form data via curl using POST?

I need to insert the following code into POST using CURL:我需要使用 CURL 将以下代码插入到 POST 中:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.bmby.com/shared/AddClient/index.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_REFERER, 'http://xxxxxxxx/');

$data = [
            'ProjectID' => xxxx
            'Password' => 'xxxxxx',
            'Fname' => $_POST['your-name'],
            'Email' => $_POST['email'],
            'Phone' => $_POST['your-tel'],
            #'Esse' => $_POST['msg'],
            'Referal' => 'http://xxxxxxxx/',
            'MediaTitle' => 'אתר אינטרנט ראשי',
            'AllowedMail' => 2, # 0 = allow, 2 = don't allow
            'IP' => $_SERVER['REMOTE_ADDR'],
];

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$output = curl_exec($ch);

Any guidance on how to implement this and have the form that we created to relate to the above code so that the service we are using to gather all the leads from the form could get the required info.关于如何实现这一点的任何指导,以及我们创建的与上述代码相关的表单,以便我们用来从表单中收集所有潜在客户的服务可以获得所需的信息。

Depending on what kind of a form you have (it could be a .php file or a WP plugin), the above code needs to be in a function or a php file that will be executed when your form is submitted, either using the action attribute in the HTML form tag or in a function that's called using a form submit action/filter (which is common if you're using a plugin).根据您拥有的表单类型(它可以是 .php 文件或 WP 插件),上述代码需要位于提交表单时将执行的函数或 php 文件中,使用action HTML 表单标签或使用表单提交操作/过滤器调用的函数中的属性(如果您使用插件,这很常见)。

The code you posted, when correctly substituted with the correct variables (for example all the URLs and whatever you pass in $data ) is the code for performing a cURL session using POST.您发布的代码,当正确替换为正确的变量(例如所有 URL 以及您传入$data任何内容)时,就是使用 POST 执行 cURL 会话的代码。 Since CURLOPT_RETURNTRANSFER is set, $output will contain the result of the cURL session on success and FALSE on failure.由于CURLOPT_RETURNTRANSFER$output将包含成功时的 cURL 会话结果和失败时的 FALSE。 Make sure that the POST variables like $_POST['your-name'] correspond to the name attributes in your form fields.确保像$_POST['your-name']这样的 POST 变量对应于表单字段中的名称属性。 It is also recommended to validate the form fields instead of directly passing the $_POST['your-name'] .还建议验证表单字段,而不是直接传递$_POST['your-name'] Once you've edited the code with the correct variable names and URLs, the code should run and the result would be in your $output .一旦您使用正确的变量名称和 URL 编辑了代码,代码应该会运行并且结果将在您的$output You can check the result to see if it was a success or do something with the result as required.您可以检查结果以查看它是否成功或根据需要对结果执行某些操作。 If you wish to get the HTTP code you should look into curl_getinfo() .如果您希望获得 HTTP 代码,您应该查看curl_getinfo()

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

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