WordPress中file_get_contents函数的替代方法

huangapple go评论182阅读模式
英文:

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

huangapple
  • 本文由 发表于 2020年1月6日 16:02:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/59608568.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定