簡體   English   中英

php-使用curl消耗此Web服務

[英]php - using curl to consume this web service

我看到了有關使用CURL 消費 Web服務的文章: 使用php消費WebService

而且我一直在嘗試遵循它,但是還沒有運氣。 我上傳了我要訪問的Web服務的照片。 給定以下示例,假設網址為:我將如何制定我的請求:

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

在此輸入圖像描述

我嘗試過此操作,但它只返回一個空白頁:

 $url = 'https://xxx.com/Spark/SparkService.asmx?op=InsertConsumer?NameFirst=Joe&NameLast=Schmoe&PostalCode=55555&EmailAddress=joe@schmoe.com&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);

確實,您可能應該看看SOAP擴展 如果它不可用或由於某種原因必須使用cURL,則這里是一個基本框架:

<?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);

?>

該服務要求您執行POST,而您正在執行GET(HTTP URL的curl默認值)。 添加這個:

curl_setopt($ch, CURLOPT_POST);

並添加一些錯誤處理:

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

這是最好的答案,因為一旦需要登錄后便使用此功能,然后從Web服務中獲取一些數據(第三方站點數據)。

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

$post = array(
    'username=abc@gmail.com',
    '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>";

暫無
暫無

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

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