1

I am working on an interface where I receive a whole bunch of event data, prepare it for another system and push it into the system via a web-based API.

Receiving and processing the data works fine, but pushing it into the other system fails, when using cURL for that. It seems like that last parameter from the querystring gets lost.

How do I know that? The interface responds and tells me that a mandatory parameter is missing. It seems to always be the last one.

This is how I build the querystring (shortened a bit)

$URI = 'http://remote-interface-host/serviceurl?';

$querystring .= 'city='.urlencode(utf8_decode($town));
$querystring .= '&street='.urlencode(utf8_decode($street));
$querystring .= '&location='.urlencode(utf8_decode($location));
$querystring .= '&start='.$start;
$querystring .= '&end='.$end;
$querystring .= '&text='.urlencode(utf8_decode(trim($description)));
$querystring .= '&title='.urlencode(utf8_decode(trim($title)));
$querystring .= '&website='.urlencode(utf8_decode($website));

$textlanguage = '&languageid=1522908220065994400';  
$querystring .= $textlanguage;

$fullcurl = $URI.$querystring;

$data = docurlcall($fullcurl);

docurlcall() is pretty simple, too

function docurlcall($url)
{
    $curl = curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_RETURNTRANSFER => 0,
        CURLOPT_TIMEOUT => 10,
        CURLOPT_URL => $url,
        CURLOPT_POST => 1,      
        CURLOPT_USERAGENT => 'my-interface'
    ));

    $result = json_decode(curl_exec($curl));

    curl_close($curl);

    return $result;
}

Feedback from the remote interface is

{ "success":false, "message":"missing required parameter 'languageid'", }

As far as I can see, this should be a fine use of cURL, so I don´t get why he drops the last parameter. But maybe that´s only a symptom of something different going wrong.

Any ideas what could be wrong or missing?

Note beside: I am stuck with cURL because the developers of the external interface fear that the data could get too big (I dropped images and more information for better readability) for a GET request, so I have to do a POST request with cURL.

flomei
  • 860
  • 1
  • 11
  • 36
  • echo `$querystring` and run directly to your browser and check what it return!! – Saty May 06 '16 at 13:14
  • That works just fine, no problems or errors, when doing so. – flomei May 06 '16 at 13:14
  • set `CURLOPT_RETURNTRANSFER =>1` and check – Saty May 06 '16 at 13:18
  • Nothing happens on the interface (means "no events are pushed into it") but I don´t see the interface feedback any more. That did not really help. – flomei May 06 '16 at 13:22
  • 2
    You say "so I have to do a POST request with cURL", but your curl setup does _not_ show that you are doing a POST. – Patrick Q May 06 '16 at 13:22
  • Did you try changing the position? When you put the language ID not in last, what happens? I think there could be some non acceptable characters like (space) in your query string. Also try to print the query string and check. Hope it will help. – Anand May 06 '16 at 13:25
  • @PatrickQ Thanks, you´re right. Added CURLOPT_POST => 1 to the function to make a POST request out of it. – flomei May 06 '16 at 13:28
  • If the remote service is specifically looking for POST data (rather than any REQUEST data), then you also need to use `CURLOPT_POSTFIELDS` – Patrick Q May 06 '16 at 13:31
  • Well, as I can enter the complete in my browser and do a successful request that way, it´s more a safety measure than anything else. – flomei May 06 '16 at 13:42
  • @Anand It then fails for some different reason. I think I really might have some bad characters left in my description or title. Damn that user data. – flomei May 06 '16 at 13:43

1 Answers1

1

Wouldn't it be a nicer setup to get rid of the query string generation and let PHP do the work for you?

$parameters = array(
  'city'        => urlencode(utf8_decode($town)),
  'street'      => urlencode(utf8_decode($street)),
  'location'    => urlencode(utf8_decode($location)),
  'start'       => $start,
  'end'         => $end,
  'text'        => urlencode(utf8_decode(trim($description))),
  'title'       => urlencode(utf8_decode(trim($title))),
  'website'     => urlencode(utf8_decode($website)),
  'languageid'  => '1522908220065994400'
);

$data = docurlcall(
  sprintf(
    "%s?%s",
    "http://remote-interface-host/serviceurl",
    http_build_query($parameters)
  )
);

This can lead to a more simpler bug search (at least more clean code leads me to more simpler error catching :)).

Luigi Pressello
  • 937
  • 4
  • 9