PHP Curl Update

I am trying to update my API with an update curl function but am struggling to work out why it isn’t working

The areas where it may be wrong is

key($id) I want it to Extract the ID column based on the key value for the ID array.

$URL i want to Create the URL based on the const variables plus the resource name plus the value of the ID array that has been passed through rawurlencode.

So far this is my update code, but am wondering what area is wrong. I can provide more info if needed and appreciate any help, thanks

function update(array $id,array $vaules, $resourcename)
{

$jsonData = json_encode($vaules);

     key($id);

$url = DOMAIN.FOLDER.APIPATH.$resourcename.rawurlencode("/".$id.$vaules);

                                $ch = curl_init($url); 

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER,array (‘content-type: application/json’));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,PUT);
curl_setopt($ch,CURLOPT_POSTFIELDS,$jsonData);

curl_exec($ch);

curl_getinfo(CURLINFO_HTTP_CODE);

        }

Hi i have just created an account for the awesome community, so am free to give you any more info if needed

Just so you know $id is a string

If $id is a string, why are you expecting an array?

Sorry I am being silly, nope it is supposed to be an array. I can give more info if needed

I think these are the problems from another blog someone said

You’re right, both key($id) and the url (the rawurlencode part) are incorrect.

key($id) (returns the index element of the current array position)[http://php.net/manual/en/function.key.php], so I need to assign it to a variable first.

Rawurlencode() itself is ok, but you’re trying to concatenate arrays using string concatenation operator . Since you’re passing json_encoded values as post, you probably don’t need values array in the url.

However all these errors would trigger a different error, so I guess your problem with “update function undefined” is caused by a missing include (that’s just a wild guess though).

But I am unsure how to fix them especially the first one. I will have another look in the morning

And sorry I didn’t use the code formatter I will do in future

Silly Bill, yes, the command key($id); will return the key that is currently being used in an array.

BUT, you have to assign it to something and then use that value. Just placing a command does nothing.

So, something like this…

$current_id = key($id);      Gets the current ID of the array $id...
$some_data = $id[$current_id];     Gets the data stored in that array in the currently used key...

Now, your URL is wrong because you are using: ("/".$id.$vaules); So, you are sticking an array of id’s
into the URL. Won’t work. $id is something like array(somekey array (somevalues)); So, it won’t work
as part of an URL… I suspect you meant to pull values out like ("/".$id[$key].$vaules[$key]);

Hope that makes sense!

Hi thanks for your help, i am still getting undefined function, please see the edit page which calls the function as well as my pseudo code.

This is my edit page, which is right but may help further identify what is wrong with the function update.

[php]if(isset($_POST[‘Submit’]))
{
$values = array();
$idValues = array($idColumn => $id);

 foreach($_POST as $key => $value)
 {
     if(!empty($value) && ($value != "Submit"))
     {
         $values[$key] = $value;
     }
 } 
 $result = update($idValues, $values, $tableName);

}
[/php]

This is my pseudo code for the function update (not for the code above but for the original update function)

Use json_encode to turn the values array into a JSON object.
Extract the ID column based on the key value for the ID array.
Create the URL based on the const variables plus the resource name plus the value of the ID array that has been passed through rawurlencode.
Use curl_init( ) to initialize a curl object.
Set the options for the CURL object by calling curl_setopt and ensuring the following options are set:
CURLOPT_URL, $URL
CURLOPT_RETURNTRANSFER, true (ensures the progress is not written to the screen)
CURLOPT_HTTPHEADER, array needs to be set as content-type : application/json
CURLOPT_CUSTOMREQUEST, PUT
CURLOPT_POSTFIELDS, JSONData
Use curl_exec( ) to return a response.
Use curl_getinfo( ) with a parameter of CURLINFO_HTTP_CODE to determine the http status code.

Thanks for your help so far i can add more info if needed

My latest code for both update and delete, getting error with update

It works for 2 tables even though it throws error on this line

[php] curl_setopt($ch, CURLOPT_CUSTOMREQUEST,‘PUT’,$output);[/php]

And with delete i am lost thanks for any help

[php]<?php
function update(array $id,array $values, $resourcename)
{

$output = json_encode($values);

$key = key($id);    

$url = DOMAIN.FOLDER.APIPATH.$resourcename.rawurlencode("/".$key); 

// Create cURL
$ch = curl_init($url); 

// Set URL and other options
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_HTTPHEADER,array ('content-type: application/json'));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,'PUT',$output);
curl_setopt($ch,CURLOPT_POSTFIELDS,$output);

// Pass URL to browser
$result = curl_exec($ch);

echo curl_getinfo($ch, CURLINFO_HTTP_CODE); 


return $output;

}
?>

<?php function delete (array $id, $resourcename) { key($id); $url = DOMAIN.FOLDER.APIPATH.$resourcename.rawurldecode($id); curl_init($curl); curl_setopt($curl, CURLOPT_URL,$url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE"); curl_exec($curl); curl.getinfo(CURLINFO_HTTP_CODE); } ?>

[/php]

Bill, sorry, but, your code makes no sense at all. did you get this off of the internet?

If you call a function such as your “update()” or “delete()”, and you use arrays as inputs, then,
you are sending the entire array to the function. Therefore, you need to parse thru the arrays
to handle the data inside them. You can NOT just use them as-is…

As an example…

$ernie=array(1,2,3,4,5);
$results_from_function=update($ernie,$ernie,"some_resource);

This will send array(1,2,3,4,5) to the update function you have written.
The update function does not have a key set as you never set it.
You would need to loop thru the array of data that is sent to the function with a for function. Like:
foreach($id as $key=>$value) {
// Do something with the keys and values…
}
and the same for the values…
You can’t just send an array to a function without handling the arrays… This code makes no sense.
Either change the calling code which you did not show us, where your code calls the two functions to send
one ID at a time or you have to alter your functions to look inside the arrays and pull out each of the ID’s
and deal with them one at a time…

Also, line #37 is still bad in the last code you posted… Does nothing!

Thanks for your help, nope i am writing it from my pseudo code

Here’s an updated version of my update function which half works

[php]<?php
function update(array $id,array $values, $resourcename)
{

$output = json_encode($values);

$id[key($id)];


$url = DOMAIN.FOLDER.APIPATH.$resourcename.rawurlencode("/".key($id)); 

// Create cURL
$ch = curl_init($url); 

// Set URL and other options
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_HTTPHEADER,array ('content-type: application/json'));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,'PUT',$output);
curl_setopt($ch,CURLOPT_POSTFIELDS,$output);

// Pass URL to browser
 curl_exec($ch);

echo curl_getinfo($ch, CURLINFO_HTTP_CODE); 


return $output;

}
?> [/php]

And heres my edit page which calls the update function, I want to update the selected row within the table. I have a get one fucntion that i used to select the edit box which works fine but then when i update it should update

[php] if(isset($_POST[‘Submit’]))
{
$values = array();
$idValues = array($idColumn => $id);

 foreach($_POST as $key => $value)
 {
     if(!empty($value) && ($value != "Submit"))
     {
         $values[$key] = $value;
     }
 } 
 $result = update($idValues, $values, $tableName);

}
[/php]

Anyway thanks fr your help above i will start looking into the problems

Well, Bill, I always try to look at the problem logically… Here is what I see in your system…

First, you scan thru the posted data which is an array of data.
During that loop, you copy the data already in an array and place into another array.

Next, you call a function to take the second array and create URL’s from ONE and only one item in it.

Does that make sense to you?

Now, you check if the submit button is pressed. Then, if it is, you parse thru the $_POST array and create
a new one. Why? Why not just use the $_POST array as it is already there. Place your function code in
that loop and there is no need to call a function. Since you pull the $key and $value out of the $_POST
array, you already have them to run thru the function code with.

Restoring an array into another, then calling a function that takes the values out of the second array is just
a waste of server resources. It just does not make sense to me why you would want to do that…

Perhaps I do not understand some other reason for this code…

Without the extra array(s) and the function, your submit button code would be just:
if(isset($_POST[‘Submit’]))
{
foreach($_POST as $key => $value)
{
if(!empty($value) && ($value != “Submit”))
{
// code from your function…
$url = DOMAIN.FOLDER.APIPATH . $resourcename.rawurlencode("/" . $key);

         // Create cURL
         $ch = curl_init($url); 
 
         // Set URL and other options
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
        curl_setopt($ch, CURLOPT_HTTPHEADER,array ('content-type: application/json'));
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST,'PUT',$output);
        curl_setopt($ch,CURLOPT_POSTFIELDS,$output);
 
       // Pass URL to browser
       curl_exec($ch);
       echo curl_getinfo($ch, CURLINFO_HTTP_CODE);
      }
  }

No need for calling a function and hashing out inputs in a new array…
$result = update($idValues, $values, $tableName);
Not sure of your CURL code. But, this makes more sense to me. The other way would be to call your
function code for each of the posted entries, not the entire array of them… To do that, it would be like:
if(isset($_POST[‘Submit’]))
{
foreach($_POST as $key => $value)
{
if(!empty($value) && ($value != “Submit”))
{
$result = update($key, $value, $tableName);
}
}
}
And, then in the function, handle just one ID’s key/value at a time…

Hope this make sense…

Sponsor our Newsletter | Privacy Policy | Terms of Service