英文:
Alternate of file_get_contents function in WordPress
问题
以下是您提供的代码的中文翻译部分:
在WordPress联系我们表单中,我需要向API发送请求。我已经在PHP中编写了以下代码并进行了测试,它正常工作。但是,当我将此代码添加到WordPress内的函数中时,我发现file_get_contents不起作用。我尝试了不同的WordPress函数,如wp_remote_post等。然后调用API将失败。请帮助我发送一个良好的请求,以便我可以发布数据。
function send_api_request(){
$postData = array(
"AccessKey" => "xxxxxxxxxx",
"Subject" => "sample subject 1",
"Name" => "sample name",
"Message" => "sample message",
"Phone" => "0000000000",
"Email" => "xxxxxx@gmail.com",
"Company" => "sample company",
"SourceFrom" => 1
);
// 创建请求的上下文
$context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => "Content-Type: application/json\r\n",
'content' => json_encode($postData)
)
));
// 发送请求
$response = file_get_contents('http://someURL.com/api/lead', FALSE, $context);
// 检查错误
if($response === FALSE){
die('Error');
}
return $response;
}
// 与WordPress尝试的代码如下。
function send_api_request($name,$email,$phone,$company,$message,$source){
$postData = array(
"AccessKey" => "xxxxx",
"Subject" => "",
"Name" => $name,
"Message" => $message,
"Phone" => $phone,
"Email" => $email,
"Company" => $company,
"SourceFrom" => $source
);
// 创建请求的上下文
$context = (array(
'http' => array(
'method' => 'POST',
'header' => "Content-Type: application/json",
'content' => json_encode($postData)
)
));
// 发送请求
$response = "";
$response = wp_remote_post("http://someURL.com/api/lead", $context);
}
英文:
I have to send a request to API
from wordpress
contact us form. I have write the following code in PHP
and test it. It was working fine. But when I add this code in a function within wordpress
I become to know that file_get_contents not working. I have tried different wordpress
function like wp_remote_post
etc. Then calling to API going to fail. please help me to send a good request so I can post data.
function send_api_request(){
$postData = array(
"AccessKey" => "xxxxxxxxxx",
"Subject" => "sample subject 1",
"Name" => "sample name",
"Message" => "sample message",
"Phone" => "0000000000",
"Email" => "xxxxxx@gmail.com",
"Company" => "sample company",
"SourceFrom" => 1
);
// Create the context for the request
$context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => "Content-Type: application/json\r\n",
'content' => json_encode($postData)
)
));
// Send the request
$response = file_get_contents('http://someURL.com/api/lead', FALSE, $context);
// Check for errors
if($response === FALSE){
die('Error');
}
return $response;
}
The code which I have tried with wordpress
is as follow.
function send_api_request($name,$email,$phone,$company,$message,$source){
$postData = array(
"AccessKey" => "xxxxx",
"Subject" => "",
"Name" => $name,
"Message" => $message,
"Phone" => $phone,
"Email" => $email,
"Company" => $company,
"SourceFrom" => $source
);
// Create the context for the request
$context = (array(
'http' => array(
'method' => 'POST',
'header' => "Content-Type: application/json",
'content' => json_encode($postData)
)
));
// Send the request
$response = "";
$response = wp_remote_post("http://someURL.com/api/lead", $context);
}
答案1
得分: 1
Sure, here is the translated code portion:
function send_api_request($name, $email, $phone, $company, $message, $source) {
$postData = array(
"AccessKey" => "xxxxx",
"Subject" => "",
"Name" => $name,
"Message" => $message,
"Phone" => $phone,
"Email" => $email,
"Company" => $company,
"SourceFrom" => $source
);
// Send the request
$response = "";
$response = wp_remote_post("http://someURL.com/api/lead", array(
'method' => 'POST',
'timeout' => 45,
'redirection' => 5,
'httpversion' => '1.0',
'blocking' => true,
'headers' => array("Content-Type" => "application/json"),
'body' => json_encode($postData),
'cookies' => array()
));
}
Please note that I've translated the code comments and variable names as well for clarity.
英文:
Try this code
function send_api_request($name,$email,$phone,$company,$message,$source){
$postData = array(
"AccessKey" => "xxxxx",
"Subject" => "",
"Name" => $name,
"Message" => $message,
"Phone" => $phone,
"Email" => $email,
"Company" => $company,
"SourceFrom" => $source
);
// Send the request
$response = "";
$response = wp_remote_post("http://someURL.com/api/lead", array(
'method' => 'POST',
'timeout' => 45,
'redirection' => 5,
'httpversion' => '1.0',
'blocking' => true,
'headers' => array("Content-Type" => "application/json"),
'body' => json_encode($postData),
'cookies' => array()
));
}
答案2
得分: -1
你可以使用wp_remote_get
函数,而不是file_get_content
。在使用之前,请阅读这里的详细信息。
谢谢。
英文:
You can use wp_remote_get
function instead of file_get_content
. Please read the details here, before using it.
Thanks
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论