CURL> API =没有回复

时间:2017-08-30 22:46:53

标签: php json wordpress api curl

我们从wordpress表单发布到API。 到目前为止我们所阅读的所有内容似乎都表明这应该有效。 所有CURL配置似乎都在那里,当通过邮递员传递JSON时,我们得到了响应。

但是我们没有通过这个CURL实现获得响应。 请指教。

    add_action( 'wpcf7_before_send_mail', 'CF7_pre_send' );

function CF7_pre_send($cf7) {

$data = array(
        "user" => array(
            "first_name" => "Some",
            "surname" => "Guy",
            "mobile_number" => "0724717299",
            "email" => "someguy@a.com",
            "skype_username" => "john.doe",
            "twitter_handle" => "test.john",
            "linkedin" => "John Matthews Doe",
            "slack_username" => "johndoe",
            "receive_marketing_info" => true,
            "accept_terms_and_conditions" => true,
            "green_member" => false,
            "industry" => "Industry 1",
            "nda" => true,
            "relationship_type" => "Partner",
            "communication_type" => "Newsletter"
        ),
        "company" => array(
            "company_name" => "Test Company",
            "website_url" => "www.test.com",
            "contact_number" => "0211111111",
            "email" => "test@test.com",
            "address_line_1" => "450 Test Strees. Testville",
            "story" => "We specialize in test of testing",
            "service_type" => "Fintech"
        )
);

    $data_string = json_encode($data);
    $username = 'USERNAME GOES HERE';
    $password = 'PASSWORD GOES HERE';

    $ch = curl_init("API URL GOES HERE");
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($ch);

    file_put_contents("cf7outputtest.txt", $result);

   return $result;
}

1 个答案:

答案 0 :(得分:0)

试试这个:

    $ch = curl_init();
    curl_setopt( $ch, CURLOPT_URL, "url-goes-here");
    curl_setopt( $ch, CURLOPT_POST, true);
    curl_setopt( $ch, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
    curl_setopt( $ch, CURLOPT_HEADER, 1);
    curl_setopt( $ch, CURLOPT_USERPWD, $username . ":" . $password);
    curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, FALSE );
    curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, FALSE );

    $result = curl_exec($ch) ;
    if( curl_error( $ch ) )
    {
      echo "Error occurred " . curl_error( $ch );
    }

    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    if ( $httpCode != 200 )
    {
        echo "Return code is {$httpCode} \n"
        .curl_error($ch);
    } 
     else {
        echo "<pre>".htmlspecialchars( $result )."</pre>";
    }

    var_dump($result);

NJOY