Php WordPress中文件获取内容功能的替代
我必须从Php WordPress中文件获取内容功能的替代,php,wordpress,Php,Wordpress,我必须从wordpress联系我们表单向API发送请求。我已经在PHP中编写了以下代码并对其进行了测试。它工作得很好。但是,当我在wordpress中的函数中添加此代码时,我就知道文件内容不起作用。我尝试过不同的wordpress函数,比如wp\u remote\u post等,然后调用API就会失败。请帮我发送一个好的请求,以便我可以发布数据 function send_api_request(){ $postData = array( "AccessKey" =>
wordpress
联系我们表单向API
发送请求。我已经在PHP
中编写了以下代码并对其进行了测试。它工作得很好。但是,当我在wordpress
中的函数中添加此代码时,我就知道文件内容不起作用。我尝试过不同的wordpress
函数,比如wp\u remote\u 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
);
// 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;
}
我用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
);
// 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);
}
您可以使用
wp\u remote\u get
功能,而不是file\u get\u content
。请在使用前阅读详细信息
谢谢试试这段代码
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()
));
}
谢谢Jinesh,现在我没有收到错误消息。早些时候我得到了。但收到API消息“AccessKey错误,请联系Transpire管理员”。尽管我正在传递正确的AccessKey,并且该密钥正在处理文件\u get\u内容。