2011-09-07 115 views
2

我看到消費使用curl Web服務這篇文章:Consume WebService with phpPHP - 使用curl消費該Web服務

,我試圖跟隨,但還沒有運氣。我上傳了我想訪問的Web服務的照片。我將如何制定我的要求給出下面的例子中,假設URL是:

https://site.com/Spark/SparkService.asmx?op=InsertConsumer

enter image description here

我嘗試這一點,但它只是返回一個空白頁:

$url = 'https://xxx.com/Spark/SparkService.asmx?op=InsertConsumer?NameFirst=Joe&NameLast=Schmoe&PostalCode=55555&[email protected]&SurveyQuestionId=76&SurveyQuestionResponseId=1139'; 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    $result = curl_exec($ch); 
    curl_close($ch); 

    $xmlobj = simplexml_load_string($result); 
    print_r($xmlobj); 
+1

幾乎但並非相當的重複數據刪除:http://stackoverflow.com/questions/7326589/php-how-to-make-a-proxy-for-a-web-service –

+0

該職位已被刪除。 – mheavers

回答

5

真的,你應該看看SOAP extension。如果不可用,或者出於某種原因,你必須使用捲曲,這裏是一個基本框架:

<?php 

    // The URL to POST to 
    $url = "http://www.mysoapservice.com/"; 

    // The value for the SOAPAction: header 
    $action = "My.Soap.Action"; 

    // Get the SOAP data into a string, I am using HEREDOC syntax 
    // but how you do this is irrelevant, the point is just get the 
    // body of the request into a string 
    $mySOAP = <<<EOD 
<?xml version="1.0" encoding="utf-8" ?> 
<soap:Envelope> 
    <!-- SOAP goes here, irrelevant so wont bother writing it out --> 
</soap:Envelope> 
EOD; 

    // The HTTP headers for the request (based on image above) 
    $headers = array(
    'Content-Type: text/xml; charset=utf-8', 
    'Content-Length: '.strlen($mySOAP), 
    'SOAPAction: '.$action 
); 

    // Build the cURL session 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_POST, TRUE); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $mySOAP); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 

    // Send the request and check the response 
    if (($result = curl_exec($ch)) === FALSE) { 
    die('cURL error: '.curl_error($ch)."<br />\n"); 
    } else { 
    echo "Success!<br />\n"; 
    } 
    curl_close($ch); 

    // Handle the response from a successful request 
    $xmlobj = simplexml_load_string($result); 
    var_dump($xmlobj); 

?> 
+0

你在評論中的含義是什麼 - 肥皂無關緊要?爲什麼更好地使用SOAP擴展? – mheavers

+0

嗯,我假設你可以自己生成XML - 它與生成HTML沒有任何區別 - 並且你需要知道如何將它發送到服務器,這就是上面所說明的。我會說使用SOAP擴展會更好的原因是因爲它是一個專門爲處理這個特定任務而設計的框架,並且有助於捕獲任何錯誤併爲您填補空白。說實話,我討厭SOAP,我認爲這與一般工作是一個噩夢。作爲一個經驗法則,如果您正在進行的任務有一個擴展名,這可能是最好的方法。 – DaveRandom

+0

是的 - 我也討厭它... – mheavers

2

的服務需要你做一個POST,而你正在做一個GET(curl的默認HTTP網址)。補充一點:

curl_setopt($ch, CURLOPT_POST); 

,並添加一些錯誤處理:

$result = curl_exec($ch); 
if ($result === false) { 
    die(curl_error($ch)); 
} 
0

這是最好的答案,因爲使用此,一旦你需要登錄然後 得到web服務的一些數據(第三方現場數據)。

$tmp_fname = tempnam("/tmp", "COOKIE"); //create temporary cookie file 

$post = array(
    '[email protected]', 
    'password=123456' 
); 

$post = implode('&', $post); 

//login with username and password 
$curl_handle = curl_init ("http://www.example.com/login"); 

//create cookie session 
curl_setopt ($curl_handle, CURLOPT_COOKIEJAR, $tmp_fname); 

curl_setopt ($curl_handle, CURLOPT_RETURNTRANSFER, true); 

curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $post); 

$output = curl_exec ($curl_handle); 

//Get events data after login 
$curl_handle = curl_init ("http://www.example.com/events"); 

curl_setopt ($curl_handle, CURLOPT_COOKIEFILE, $tmp_fname); 

curl_setopt ($curl_handle, CURLOPT_RETURNTRANSFER, true); 

$output = curl_exec ($curl_handle); 

//Convert json format to array 
$data = json_decode($output); 

echo "Output : <br> <pre>"; 

    print_r($data); 

echo "</pre>";