I have to make a post request and have done the same using postman for testing. Now, the code base is using php and I have never used php ever before. Upon checking the code section in postman, I got this:
CURLOPT_POSTFIELDS =>'{"datalist":[{
"Firstname":"firstname",
"Lastname":"lastname4",
"Email":"lead#test2.com",
"Phone":"9899999999",
"leadsource":"Website",
"address":"lakeview",
"pincode":"440010",
"Grade":"Class 1",
"utmcampaign":"summer_sale",
"utmcontent":"video_ad",
"utmmedium":"organic_social",
"utmterm":"social_media",
}
]}'
The problem is that I have to pass dynamic values instead of firstname, lastname4 etc.
The values I have are stored as such $lead['name'].
So, basically I have to pass $lead['name'] instead of lastname4 but I am not able to figure out the syntax.
You can use json_encode on an array to build the desired JSON string
$arr=array(
'datalist'=>array( array(
'Firstname'=>$lead['name'],
'Lastname'=>$lead['last'],
'Email'=>$lead['email'],
'Phone'=>$lead['phone'],
'leadsource'=>$lead['website'],
/*... etc */
)
) );
CURLOPT_POSTFIELDS=json_encode( $arr );
Related
I'm trying to build a page that use API from another site. I'm kind noob on this so my question can be silly.
This site have a bunch of category and on each of this category there are different fields that have to be filled so you can post an item.
So instead of me creating a page for each category, and try to pass a get that is already available on their API get the variable that have Required as it value so I can pass it to my code to fill it the right way.
So if I pass:
curl -X GET -H 'Authorization: Bearer $ACCESS_TOKEN' https://api.mercadolibre.com/categories/MLA1055
It returns me a lot of results, and on some of it are:
"shipping_profile": "optional",
"show_contact_information": false,
"simple_shipping": "optional",
"stock": "required"
So my idea is if there is a way to get the the name of the result that will be stock. So I can put it on my code so the user can fill it and I don't have to do category by category...
Like I said before. I'm noob at PHP and cURL, but I have some experience with coding, is just a new language, hope you guys can help me.
First, you would have to convert the JSON string from API into an object understood by PHP, we have json_decode() for this:
//let's assume JSON data has been downloaded and stored in data.json file
$jsonString = file_get_contents(dirname(__FILE__) . '/data.json');
$jsonObject = json_decode($jsonString, true);
Next, from the URL you gave in the comment, I can see that the "required" data is stored inside settings key. You can use JSON viewer tools to visualize the data.
I'm using online JSON viewer tools
Extract it to another variable, or just loop through the elements:
$arrayRequired = array();
foreach($jsonObject['settings'] as $key => $setting){
if($setting === 'required'){
$arrayRequired[] = $key;
}
}
Your required keys now stored inside $arrayRequired variable:
print_r($arrayRequired);
Array
(
[0] => immediate_payment
[1] => price
[2] => stock
)
That's it.
I am having difficulty with the proper layout for this piece of code I have been talking with another IT support person and we are both at a bit of a loss
I am trying to utilize an API call and I am trying to come up with the proper syntax
I am using a PHP POST call and CURL along with an array of variable names and values for the most part it works
The problem is that a few variable names are not recognized and the other support person thought we needed to change things up a bit
The documentation states there may be a change we need to setup these variable ahead of time
the structure needs to be something like this CDATA block:
<profileFieldValues>
<fieldValue id="_sys_firstname">
<value>Jeff</value>
</fieldValue>
<fieldValue id="_sys_lastname">
<value>Johnson</value>
</fieldValue>
</profileFieldValues>
WE are using a structure like this
$fields = array(
'restype' => '2',
'username' => 'abc123',
...,
)
So what would the profileFieldValues block of code be written to work in the $fields array?
We were trying something like this but it was not correct
'profileFieldValues'=>"fieldValue id[]=>('_sys_firstname=>'value'=>'Charlie','_sys_lastname=>'value'=>'Brown')",
we received an error with this:
The user profile field elements are not properly formatted
I'm trying to perform a mass assignment of 2 variables I'm sending via GET to another model::controller (from project::actionCreate to client::actionCreate)
In the _form view for project::actionCreate I've got the following:
<?php echo " ".Chtml::link('+New client',array('client/create',array('Client' => array('redir'=>Yii::app()->controller->route,'redirId'=>$model->id))));?>
With the goal of creating an array "Client" with attributes "redir" and "redirId".
In client::actionCreate I want to do something like
if(isset($_GET['Client']))
{
$model->attributes=$_GET['Client'];
}
Now I noticed that my $_GET var puts client inside subarray 0, so I've tried this with
$_GET[0]['Client']
as well, but no luck. However if I manually assign the variables like this:
$model->redir = $_GET[0]['Client']['redir'];
$model->redirId = $_GET[0]['Client']['redirId'];
Then it works.
Any idea what is up? The goal is to allow someone to create a new client while creating/updating a project record, by sending them to client::actionCreate, but redirecting them back to their original project::actionCreate if they were linked there from my "+New Client" link.
I think the client array is put inside subarray 0 because you've added an array around the parameters. Try removing the array like the following:
<?php
Chtml::link('+New client',array('client/create', 'Client' => array('redir'=>Yii::app()->controller->route,'redirId'=>$model->id)));
?>
I don't know what your model looks like but if the fields aren't assigned they are probably not safe. You can make them safe by adding them to the rules part of your model. Or you could try the following, by specifying the false parameter it will be possible to assign values to unsafe attributes. (http://www.yiiframework.com/doc/api/1.1/CModel#setAttributes-detail)
$model->setAttributes($_GET['Client'], false);
I am not sure creating a link like you want is possible. I have asked something similar some time ago Yii link with [ as a parameter I just could never get the link to how I wanted it. In the end I just created the link the old fashion way, not using CHTML.
I have three different request for my ajax:
$result = Map_Model_Map_Factory::getCityByRegionAlias($alias);
$resultCountUsers = User_Model_User_Factory::countUserByRegion($alias);
$resultCountPartners = User_Model_User_Factory::countPartnersByRegion($alias);
First request works pretty well. But second and third conflicts with each other. If $this->_helper->json($resultCountUsers); comes first, then it works:
$this->_helper->json($resultCountUsers);
$this->_helper->json($resultCountPartners);
$this->_helper->json($result);
I get what I need countUsers: "1" but I don't have countPartners. And vice versa, if $this->_helper->json($resultCountPartners); comes first, then I get countPartners without countUsers.
Maybe somebody know, what's going on and how I can receive that.
I don't use Zend, but there is clearly a problem: you are not providing attribute names for the JavaScript object. I wonder whether you are overwriting each response with the next one.
See what effect this has in your AJAX viewer:
$this->_helper->json(
array(
'resultCountUsers' => $resultCountUsers,
'resultCountPartners' => $resultCountPartners,
'result' => $result,
)
);
I'm looking for a simple example of some code showing how to use jquery autocomplete with ids and values, json and php, but I can't find any. Basically I have a list of employee names in a database, they have an id and a name. I would like to display the name, but use the id in a hidden field in my form submission. I would like to provide the list of names via php and json ajax. I would also like to use the feature where you have to enter say 3 characters before it shows the autocomplete, since it is a very long list of employee names. Not sure whether you have to pass the 3 characters to the php as a parameter for this.
I'm a noob so a simple example would really be appreciated.
Thanks
There are great examples here at jQuery UI:
http://jqueryui.com/demos/autocomplete/#remote
Click the view source button below the example to get a look at the Javascript side of things. Note the parameters for "source" (this is the path to your PHP script delivering results) and "minLength." Setting the minLength property to 3 will take care of the second part of your question.
As far as the remote side the results need to be in JSON format so you can query your data from the database, get it as a PHP associative array and then use the json_encode() method to put it into a format that the autocomplete plugin can read. The autocomplete implementation in their example sends a querystring variable, "term", to the source file containing the search string input by the user.
This example expects JSON results from the server to be in this format:
[ { "id": "Branta hrota", "label": "Pale-bellied Brent Goose", "value": "Pale-bellied Brent Goose" }, ...]
So the PHP source page could deliver results by using code like this:
// source.php (or whatever path you used for the autocomplete "source" setting)
// Sanitize your input!
$term = mysql_real_escape_string( $_GET['term'] );
$results = mysql_query( "SELECT * FROM employees WHERE name LIKE '%{$term}%' "); // Grab your data
$output_array = new array();
while ( $row = mysql_fetch_assoc( $results ) ) {
$output_array[] = array(
'id' => $row['id']
, 'label' => $row['name']
, 'value' => $row['name']
);
}
// Print out JSON response
echo json_encode( $output_array );
So that's all untested pseudocode, but it should point you in the right direction.