如何创建带有值变量的 php 键控数组
我正在使用 drupal 创建一些提交给第三方的自定义表单,并且我正在尝试捆绑我的表单结果(其中一些结果应用了一些转换函数)以发送给表单收件人。
我已经在简单的表单上使用了 Drupal 的 drupal_http_request 函数,并且它运行良好,但是现在我的一些结果为 var 赋予了值,我不确定如何创建一个继续传递 var 值的键控数组。
我知道,当我将表单直接发送给第三方时,他们会收到正确的结果,但是我试图向他们发送结果,但会重定向到我自己网站中的页面,所以我知道我的表单至少有效 - 而且它是只是使用尝试编码的这个功能,所以我可以控制没有的重定向。 但我什至不知道键控数组是否可以具有变量作为值,或者是否是语法问题
这是我的代码:
function formshare_nameform_submit($form_id, &$form_state) {
// Lookup share matrix return value
$shareMatrixValue = setShareMatrixValue($form_state['values']['image_id']);
$langResultsCode = setLangForShare($langCode);
$gender = setGenderForShare($form_state['values']['gender']);
$first_name = $form_state['values']['first_name'];
$last_name = $form_state['values']['last_name'];
$email = $form_state['values']['email'];
$email_friend1 = $form_state['values']['email_friend1'];
$email_friend2 = $form_state['values']['email_friend2'];
$email_friend3 = $form_state['values']['email_friend3'];
$message = $form_state['values']['massage'];
$optin = $form_state['values']['optin'];
$category = $form_state['values']['category'];
$image_id = $shareMatrixValue;
$id_langue = $form_state['values']['id_langue'];
// Make an array of values to pass to 3rd party.
$pass_these = array();
$pass_these['shareMatrixValue'] = $shareMatrixValue;
$pass_these['langResultsCode'] = $langResultsCode;
$pass_these['gender'] = $gender;
$pass_these['first_name'] = $first_name;
$pass_these['last_name'] = $last_name;
$pass_these['email'] = $email;
$pass_these['email_friend1'] = $email_friend1;
$pass_these['email_friend2'] = $email_friend2;
$pass_these['email_friend3'] = $email_friend3;
$pass_these['message'] = $message;
$pass_these['optin'] = $optin;
$pass_these['category'] = $category;
$pass_these['image_id'] = $image_id;
$pass_these['id_langue'] = $id_langue;
// Post the data to 3rd party.
$url = 'http://mythirdpartysite.com';
$headers = array('Content-Type' => 'application/x-www-form-urlencoded');
$data = drupal_query_string_encode($pass_these);
drupal_http_request($url, $headers, 'POST', $data);
}
I’m using drupal to create some custom form that submit to a third party, and I’m trying to bundle my form results - some of which have some conversion functions applied to them - to send to the form recipient.
I’ve used Drupal’s drupal_http_request function on simple forms and its’ worked well, but now that some of my results give value to vars, I’m not sure how to create a keyed array that continues to pass the value of the var.
I know that when I send my form directly to my third party, they receive the correct result, however I am trying to send them the results but redirect to a page in my own site, so i know my form works at least - and it’s just this function of using the trying encode so i have control over redirect that doesn’t.
but I don't even know if a keyed Array can have variables as it's values or not, or if it's a matter of syntax
This is my code:
function formshare_nameform_submit($form_id, &$form_state) {
// Lookup share matrix return value
$shareMatrixValue = setShareMatrixValue($form_state['values']['image_id']);
$langResultsCode = setLangForShare($langCode);
$gender = setGenderForShare($form_state['values']['gender']);
$first_name = $form_state['values']['first_name'];
$last_name = $form_state['values']['last_name'];
$email = $form_state['values']['email'];
$email_friend1 = $form_state['values']['email_friend1'];
$email_friend2 = $form_state['values']['email_friend2'];
$email_friend3 = $form_state['values']['email_friend3'];
$message = $form_state['values']['massage'];
$optin = $form_state['values']['optin'];
$category = $form_state['values']['category'];
$image_id = $shareMatrixValue;
$id_langue = $form_state['values']['id_langue'];
// Make an array of values to pass to 3rd party.
$pass_these = array();
$pass_these['shareMatrixValue'] = $shareMatrixValue;
$pass_these['langResultsCode'] = $langResultsCode;
$pass_these['gender'] = $gender;
$pass_these['first_name'] = $first_name;
$pass_these['last_name'] = $last_name;
$pass_these['email'] = $email;
$pass_these['email_friend1'] = $email_friend1;
$pass_these['email_friend2'] = $email_friend2;
$pass_these['email_friend3'] = $email_friend3;
$pass_these['message'] = $message;
$pass_these['optin'] = $optin;
$pass_these['category'] = $category;
$pass_these['image_id'] = $image_id;
$pass_these['id_langue'] = $id_langue;
// Post the data to 3rd party.
$url = 'http://mythirdpartysite.com';
$headers = array('Content-Type' => 'application/x-www-form-urlencoded');
$data = drupal_query_string_encode($pass_these);
drupal_http_request($url, $headers, 'POST', $data);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 &$var 通过引用存储变量,也可以简单地将变量的名称存储在数组中并使用 $$array['key'];要获得 $var 值,
最好的办法是创建一个 stdClass 对象并将变量存储为属性,它的工作方式类似于关联数组,但更干净
you can either store the variable by reference with &$var or simply store the name of the variable in the array and use $$array['key']; to get the $var value
best thing is create an stdClass object and store the vars as atributes, it works like an associative array but it's cleaner