2

I saw this post on consuming a web service using CURL: Consume WebService with php

and I was trying to follow it, but haven't had luck. I uploaded a photo of the web service I'm trying to access. How would I formulate my request given the example below, assuming the URL was:

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

enter image description here

I attempted this, but it just returns a blank page:

 $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

3 Answers 3

5

Really, you should probably look at the SOAP extension. If it is not available or for some reason you must use cURL, here is a basic framework:

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

?>
Sign up to request clarification or add additional context in comments.

2 Comments

what do you mean in your comment - the soap is irrelevant? And why is using the SOAP extension better?
Well I'm assuming you can generate the XML yourself - it's not really any different from generating HTML - and you need to know how to send it to the server, which is what the above illustrates. The reason I would say that using the SOAP extension would be better is because it is a framework specially designed for handling this specific task and will help to catch any errors and fill in the gaps for you. To be honest, I hate SOAP, I think it is a nightmare to work with in general. As a rule of thumb, if there is an extension for the task you are working on, it's probably the best approach.
2

The service requires you to do a POST, and you're doing a GET (curl's default for HTTP urls) instead. Add this:

curl_setopt($ch, CURLOPT_POST);

and add some error handling:

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

Comments

1

This is the best answer because using this once you need to login then get some data from webservices(third party site data).

$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>";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.