0

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);
}
nbhatti2001
  • 353
  • 2
  • 7
  • 33

2 Answers2

1

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()
    ));
}
nbhatti2001
  • 353
  • 2
  • 7
  • 33
Jinesh
  • 1,554
  • 10
  • 15
  • Thanks Jinesh, now I am not getting error message. Earlier I was getting. But have got API message "AccessKey is wrong Please contact Transpire Admin". Although I am passing correct AccessKey and that key is working with file_get_contents. – nbhatti2001 Jan 06 '20 at 10:14
-1

You can use wp_remote_get function instead of file_get_content. Please read the details here, before using it.

Thanks

Tristup
  • 3,603
  • 1
  • 14
  • 26