Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8949927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:19:55+00:00 2026-06-15T13:19:55+00:00

I have built an API that I want to test. By that reason I’m

  • 0

I have built an API that I want to test. By that reason I’m building a simple client to try out the different features (CRUD). Below is the function for updating a producer, which works fine. However, I also want to be able to update parts of a producer, e.g. address (/producers/8?method=put&address=milkyway).

The array producer always contains the same elements (name, address, zipcode etc) but I only want to update the producer with the elements in the array which contains of anything. What I mean with that is that if for example the name element in the array is empty then name shouldn’t be included in *http_build_query*. If only the name element contains of anything then only name should be updated.

So, let’s say that the array (except for id that of course is mandatory) contains of address. How can I dynamically add only that to *http_build_query* ?

Thanks in advance!

public function UpdateProducer($producer) {

    $url = 'http://localhost/webbteknik2/Labb2/api/v1/producers/ . $producer['id'] . '?method=put';

    $data = http_build_query(array(
        'name' => $producer['name'],
        'address' => $producer['address'],
        'zipcode' => $producer['zipcode'],
        'town' => $producer['town'],
        'url' => $producer['url'],
        'imgurl' => $producer['imgurl'],
        'latitude' => $producer['latitude'],
        'longitude' => $producer['longitude'],
    ));

    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    ...
    the rest of the curl code
}

Note: I know this is bad coding in many ways, but as I said I only, asap want to be able to test the CRUD functionality through the client.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T13:19:57+00:00Added an answer on June 15, 2026 at 1:19 pm

    use array_filter to remove the empty elements….

    $params = array(
        'name' => $producer['name'],
        'address' => $producer['address'],
        'zipcode' => $producer['zipcode'],
        'town' => $producer['town'],
        'url' => $producer['url'],
        'imgurl' => $producer['imgurl'],
        'latitude' => $producer['latitude'],
        'longitude' => $producer['longitude'],
    );
    
    $data = http_build_query(array_filter($params, 'is_null'));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have built an API framework that includes a lot of communication-related options, including
I have a mini API that is only for an app I have built.
We have a website that we want to provide web based API access to
We have a scenario where we have built an API that we will provide
I have built a REST API backend whith Spring MVC and secured with basic
We have an API built on Rails, and processing some stuff with resque. After
I have a REST API built with Symfony2 and the FOSRestBundle. This all works
I have a web application built in Django 1.3. We have also provided API
I have built so far an application that allows the user to drag and
I have built a component library that includes an executive class which does work

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.