0

I am new to web services. I have tried to request from the example web service at http://www.w3schools.com/webservices/tempconvert.asmx. This is the code I have tried:

<?php
    $data = array('Celsius' => '56');  

    $curl = curl_init('http://www.w3schools.com/webservices/tempconvert.asmx
    /CelsiusToFahrenheit');

    curl_setopt($curl, CURLOPT_POST, 1); 
    curl_setopt($curl, CURLOPT_URL, 'http://www.w3schools.com/webservices/tempconvert.asmx/CelsiusToFahrenheit');  
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);  
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);  

    $result = curl_exec($curl);

    curl_close($curl);

    echo $result;
?>
DisplayName
  • 3,065
  • 5
  • 36
  • 40
Anas Fahumy
  • 41
  • 1
  • 9

2 Answers2

0

I am not entirely sure, but should the data be a query string

$data = "Celsius=56";

I am sure you are doing this just to test curl. You don't really need a webservice to convert celsius to farenheit

SoWhat
  • 5,204
  • 2
  • 24
  • 54
  • It's a POST request, so it shouldn't be a query string – Mark Baker Aug 06 '13 at 10:43
  • i mean the data has to be encoded the same way. http://stackoverflow.com/questions/2138527/php-curl-http-post-sample-code – SoWhat Aug 06 '13 at 10:45
  • thanks for the replys. Just forget the code. you can see the webservice http://www.w3schools.com/webservices/tempconvert.asmx . plese guide me how to create a php request in http post method. i need the response in a variable. thanks – Anas Fahumy Aug 06 '13 at 10:51
  • It works for me if I do what Somesh says and I remove the line break from the URL in `CURLOPT_URL` statement. – ironcito Aug 06 '13 at 10:56
  • In fact, you don't need to specify the URL twice. You can do it either in the `init` or in the `setopt`, just make sure there are no line breaks. – ironcito Aug 06 '13 at 11:03
  • yay I am right I am right :). Now show some love and press those upvote buttons. Don't be shy – SoWhat Aug 06 '13 at 11:05
  • @AnasFahumy Try making the changes ironcito suggested and see if it works. There's a link in my first comment – SoWhat Aug 06 '13 at 11:06
  • @SomeshMukherjee thanks alot for the link. I could solve the issue. Thanks. – Anas Fahumy Aug 07 '13 at 03:17
0
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, Array("Content-Type: application/x-www-form-urlencoded"));
curl_setopt($curl, CURLOPT_POST,count($data));
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));

Use these attibutes. to fetch the data. as some url requires exact parameters.
Note* :- Headers are optional

Priya
  • 1,416
  • 1
  • 14
  • 29