1

I am trying to write PHP code for following API:

curl -u ********* -d email=tester@gmail.com \
--data-urlencode msg="First message from domain API" \
--data-urlencode url="http://www.domain.com/welcome" \
https://api.domain.com/1/send 

PHP Code:

<?php
$url = "https://api.domain.com/1/send ";

$ch = curl_init();    

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

curl_setopt($ch, CURLOPT_URL, $url); 

curl_setopt($ch, CURLOPT_FAILONERROR, 1);

curl_setopt($ch, CURLOPT_USERPWD, "*********"); 

curl_setopt($ch, CURLOPT_POST, 1); // set POST method

curl_setopt($ch, CURLOPT_POSTFIELDS, "email=test@outlook.com&msg=hi%url=http://yahoo.com/"); // add POST fields

$result = curl_exec($ch); 

curl_close($ch); 

echo $result;       

?>

What am I doing wrong to process the above API?

durron597
  • 31,968
  • 17
  • 99
  • 158
seoppc
  • 2,766
  • 7
  • 44
  • 76
  • [How to debug curl requests](http://stackoverflow.com/questions/3757071/php-debugging-curl) – Manse Dec 18 '12 at 14:06
  • 2
    what's the problem? Are you getting any errors? Anything value for $result? It's hard to know what you need without telling us what's wrong. – thescientist Dec 18 '12 at 14:06
  • By saying "curl api isnt responding anything" you mean that `$result` is `false`? If `$result` is an empty string, you should print out and post here the server's response headers. – Eugene Dec 18 '12 at 14:18

1 Answers1

0

Try this:

curl_setopt($ch, CURLOPT_POSTFIELDS, urlencode("email=test@outlook.com&msg=hi&url=http://yahoo.com/"));

There may be other errors (I can't easily test your code) but that should help. Notice that, in addition to adding the urlencode function, I also changed a % to a &

This question will probably help you, too.

Community
  • 1
  • 1
durron597
  • 31,968
  • 17
  • 99
  • 158
  • better suggestion is to just put the post fields into an array and let curl do the encoding for you. it'll happily accept an array. – Marc B Dec 18 '12 at 14:48
  • @durron597 its still not returning anything, do you need complete code to test? – seoppc Dec 18 '12 at 15:12
  • @seoppc I recommend following my link and following Marc B's advice if you haven't yet. – durron597 Dec 18 '12 at 15:42
  • @durron597 i have tried that, please find complete api curl code at http://codepad.org/UPiCklPj so you could have a test as well, thanks for your efforts. – seoppc Dec 18 '12 at 16:35