flomei
flomei

Reputation: 880

Parameter gets "lost" when using cURL

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.

Upvotes: 1

Views: 1524

Answers (1)

Luigi Pressello
Luigi Pressello

Reputation: 935

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 :)).

Upvotes: 1

Related Questions