1

I'm trying to save an API Rest response in entry with PHP. This is the functional PHP script:

{exp:channel:entries 
   channel="products" 
   limit="1"
   require_entry="yes"
   entry_id="{segment_3}"
}
<?php

/**
 * Ensure that you have cURL installed by uncommenting
 * ;extension=php_curl.dll
 * in your php.ini
 */

$url = "https://api.**************";

{product_uploaded_model}
$path = $_SERVER['DOCUMENT_ROOT'] . '/models/uploads/products/';
$filename = "{file_name}";
$tags = "{author} {file_id}";
$description = "{title}";
{/product_uploaded_model}
$token_api = "*********************";
$title = "{title}";
$private = 1;
$password = "****";

$data = array(
    "title" => $title,
    "description" => $description,
    "fileModel" => "@".$path.$filename,
    "filenameModel" => $filename,
    "tags" => $tags,
    "token" => $token_api,
    "private" => $private,
    "password" => $password
);

$ch = curl_init();
curl_setopt_array($ch, array(
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => $url,
    CURLOPT_POST => 1,
    CURLOPT_POSTFIELDS => $data
));

$response = curl_exec($ch);

curl_close($ch);

echo $response;

?>

{/exp:channel:entries}

The server's response is like this

{"result": {"id": "32c45b4fe5b74420a53ee8e76c247837"}, "success": true}

Now i need to put the "id" in a entry's field with this in single entry template:

{exp:channel:form channel="*****" return="*****/ENTRY_ID" entry_id="{segment_3}"}

{field:product_id}

{/exp:channel:form}

Extrapolating the id from the response and putting it in the field, and save the form.

The question is: How can I extrapolate the id from the response? How can I put in {field:product_id}? How can I save the form and redirect to another page?

1

This StackOverflow answer shows how to extract a variable from a JSON string: https://stackoverflow.com/a/10689950/135935

Then echo that variable inside the exp:channel:form tag into a hidden field:

<input type="hidden" name="product_id" value="<?php echo $ID ?>">
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.