簡體   English   中英

該錯誤的原因是什么-無法解析PHP5 cURL中的主機

[英]What is the cause of this error - Couldn't resolve host in PHP5 cURL

我是cURL的新手。 我正在嘗試發送XML請求並以XML的形式將其響應發送到遠程服務器中的其余Web應用程序。 以下是我嘗試發送的代碼:

<?php
//header("refresh:5;url=form.html");
if(isset($_POST['create_xml'])){


$contact = "contact";
$first_name = $_POST["element_1"];
$last_name = $_POST["element_2"];
$email = $_POST["element_3"];
$country_code=$_POST["element_4_1"];
$contact_number=$_POST["element_4_2"].$_POST["element_4_3"];
$comments = $_POST["element_5"];

//if ($first_name && $last_name && $email && $contact_number && $comments) { 
//echo "Thank you for submitting your form. You may submit email service requests to our Support Center at:";
//} else { 
//exit("You have not filled out all the required fields. Place hit your back button and fill out all the required fields.");
//}

$xml = '<?xml version="1.0" encoding="UTF-8"?>';
$xml .= "<command>";
$xml .= "ADD_NEW_CONTACT";
$xml .= "</command>";
$xml .= "<data>";
$xml .= "<name>";
$xml .= $first_name.''.$last_name;
$xml .= "</name>";
$xml .= "<username>";
$xml .= $email;
$xml .= "</username>";
$xml .= "<preferredemail>";
$xml .= $email;
$xml .= "</preferredemail>";
$xml .= "<mobile>";
$xml .= "<countrycode>";
$xml .= $country_code;
$xml .= "</countrycode>";
$xml .= "<mobilenumber>";
$xml .= $contact_number;
$xml .= "</mobilenumber>";
$xml .= "</mobile>";
$xml .= "<gender>";
$xml .= "TBD";
$xml .= "</gender>";
$xml .= "</data>";
$xml .= "</groupzsyncreq>";

$xml =htmlentities($xml);
//echo $xml;


/**
     * Define POST URL and also payload
     */
    define('XML_POST_URL', 'http://www.testapp.com/test?request=');

    /**
     * Initialize handle and set options
     */
    $ch = curl_init();
    set_time_limit(0);
    curl_setopt($ch, CURLOPT_URL, XML_POST_URL);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 100);
    curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: close'));

    /**
     * Execute the request and also time the transaction
     */
    $start = array_sum(explode(' ', microtime()));
    $result = curl_exec($ch);
    $stop = array_sum(explode(' ', microtime()));
    $totalTime = $stop - $start;

    /**
     * Check for errors
     */
    if ( curl_errno($ch) ) {
        $result = 'ERROR -> ' . curl_errno($ch) . ': ' . curl_error($ch);
    } else {
        $returnCode = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
        switch($returnCode){
            case 404:
                $result = 'ERROR -> 404 Not Found';
                break;
            default:
                break;
        }
    }

    /**
     * Close the handle
     */
    curl_close($ch);

    /**
     * Output the results and time
     */
    echo 'Total time for request: ' . $totalTime . "\n";
    echo $result;  

    /**
     * Exit the script
     */
    exit(0);
}
?>   

現在,當我嘗試從本地系統發送XML請求時,出現此錯誤

Total time for request: 20.308043956757 ERROR -> 6: Couldn't resolve host 'www.testapp.com'.  But, `www.testapp.com` is fine and is up. How to solve this error.
define('XML_POST_URL', 'http://www.testapp.com/test?request=');

我認為,這還不完整。 您能否在wsdl末尾的service標簽下檢查端點URL。

在使用xml時更改此http標頭

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml'));

對於http POST也請使用此curl選項

curl_setopt($ch, CURLOPT_POST, true);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM