3

I call a webservice from a JQuery $.getJSON function, it works fine.

 var p = {
 'field1': 'value1',
 'field2': 'value2',
 'field3': 'value3'
 };
 $.getJSON('https://service:[email protected]/service/search?callback=?', p, function(data) {
 if (data[0]) { 
 // print results
 } else {
 // no results found
 }
});

I am trying to connect from PHP and CURL, however it does not work, it always return false.

//FIRST TRY

$params = array( 'field1' => 'value1', 'field2' => 'value2', 'field3'=> 'value3');
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, 'https://service:[email protected]/service/search?callback=?');
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
$result = curl_exec($ch); // return false instead of my JSON

// SECOND TRY

 $data_string = json_encode($params); 
 $ch = curl_init('https://https://service:[email protected]/service/search?callback=?'); 
 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); 
 curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); 
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
 curl_setopt($ch, CURLOPT_HTTPHEADER, array( 
 'Content-Type: application/json', 
 'Content-Length: ' . strlen($data_string)) 
); 
 $result2 = curl_exec($ch); //return false instead of my JSON

What I am doing wrong?

many thanks,

Prashant Singh
3,79313 gold badges65 silver badges106 bronze badges
asked Nov 7, 2012 at 1:16
2
  • is the request jsonp or json? The format of what's returned is different in both cases Commented Nov 7, 2012 at 1:24
  • .getJSON is a get request. In PHP you are using a post request. Also in POSTFIELDS takes an associative array but in your second try you only gave it a string. Commented Nov 7, 2012 at 1:30

3 Answers 3

1

Try changing your code to this:

$params = array( 'field1' => 'value1', 'field2' => 'value2', 'field3'=> 'value3');
$data_string = implode('&',$params);
//NB: you may need to urlencode the each of your params
$ch = curl_init('https://service:[email protected]/service/search? callback=?&' .$data_string); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
$result2 = curl_exec($ch);

Untested code, hope it helps.

answered Nov 7, 2012 at 5:46
0

The jquery request is using GET. The curl code you wrote appears to be sending a post request (i'm not an expert in curl). Obviously the receiving server handles different types of requests differently, so make sure you send a get via curl and that should help.

answered Nov 7, 2012 at 1:31
0

getJSON sends a GET request, so you need to convert params array to a string with http_build_query and append it to query. As you're requesting the data with HTTPS you need to point CURL to valid cerficate with CURLOPT_CAINFO / CURLOPT_CAPATH, i'll just ignore the validation in the code.

$params = array( 'field1' => 'value1', 'field2' => 'value2', 'field3'=> 'value3');
$url = 'https://service:[email protected]/service/search?callback=?' . http_build_query($params);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch , CURLOPT_SSL_VERIFYPEER , false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
if($result === FALSE) {
 echo curl_error($ch);
}
answered Nov 7, 2012 at 9:20

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.