Can someone take a look at how I've set up this REST call? I haven't been able to find an example of this, and I can't get it to work.
It may be the blank 'link_name_to_fields_array' parameter. I have read the documentation and don't really understand that parameter. I don't know if that is causing my problem or not.
Any help would be apprecitated.
//SET UP CURL
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
//parameters for get_entry call (I received a session id from a call to the login function. Using it here)
//I manually got this user 'id' form the sugarcrm database
$parameters = array(
'session'=>$result->id,
'module_name' => 'users',
'id' => '21a6a633-40de-9bf4-aa14-4f8753ea5aa2',
'select_fields' => array('user_name'),
'link_name_to_fields_array'=> array()
);
$json = json_encode($parameters);
$postArgs='method=get_entry&input_type=JSON&response_type=JSON$rest_data=' . $json;
curl_setopt($curl,CURLOPT_POSTFIELDS, $postArgs);
$result2 = curl_exec( $curl );
echo("<pre>" . print_r($result2,true) . "</pre>");
The output is "Bad data passed in; Return to Home"
You have an error in the postArgs line (replace $rest_data with &rest_data). Try with this:
$postArgs='method=get_entry&input_type=JSON&response_type=JSON&rest_data=' . $json;
Related
I have anonymous leads in Marketo and I need to merge them with known leads through the REST API.
I am trying the code below
public function postData() {
$url = $this->host . "/rest/v1/leads/" . $this->id ."/merge.json?access_token=" . $this->getToken() . "&leadIds=" . $this::csvString($this->leadIds);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 120,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"content-type: application/json",
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
}
It is responding with {"requestId":"1af7#fgfdhgfdgfgdfg","success":false,"errors":[{"code":"1004","message":"Lead '231325' not found"}]} but I can see the lead with the id 231325 in Marketo.
How can I merge the anonymous lead with the known lead with REST?
Unfortunately, you cannot do that, or not in a straightforward way.
The description of the Merge functionality in the Docs does not states it as clearly as the Merge Endpoint reference, but it "merges two or more known lead records into a single lead record". So even if you see the MarketoIds of these visitors in the system, the API won't find them in the Leads table.
However, depending on your exact goal and setup, you might solve your issue by using the Associate Web Activity functionality, as it "associates a known Marketo lead record to a munchkin cookie and its associated web activity history". The Associate Endpoint expects the value of the _mkto_trk cookie of a visitor (sample format: id:287-GTJ-838%26token:_mch-marketo.com-1396310362214-46169).
Having said that, your curl would look something like this:
$url = $host . '/rest/v1/leads/' . $leadId . '/associate.json?cookie=' . $cookie . '&access_token=' . $token;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
]);
$response = curl_exec($curl);
curl_close($curl);
You can obtain the value of the cookie with $_COOKIE['_mkto_trk'].
I need to fill form and then send data to the server. To simulate form filling and button submit I'm trying to use PUT method and cURL but without any success.
I have no idea what im doing wrong becouse I'm not very experienced in using cURL. Thanks to all for any help.
These headers I can see when I submit form manualy: http://www.krafty.cz/headers.txt
and here is my code:
$data = array(
'authenticity_token' => 'uGjjg9Jv4E5s4FE9e6afza9v1ycIR9sdAAnvmjcGf8YeUl0+UzpsaT5p/I/LIFs/kbOb7g4TmWx5Pq+TGt7V1Q==',
'show_preview' => '1',
'product[international]' => '0',
// *** THERE IS MUCH MORE FIELDS IN ARRAY HERE, ACCORDING TO HEADERS
'product[auto_renewable]' => 'true',
'button' => ''
);
$curl = curl_init('http://en.dawanda.com/seller/products/new');
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded','authenticity_token: uGjjg9Jv4E5s4FE9e6afza9v1ycIR9sdAAnvmjcGf8YeUl0+UzpsaT5p/I/LIFs/kbOb7g4TmWx5Pq+TGt7V1Q=='));
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
$response = curl_exec($curl);
I am trying to retrieve custom module data through the Sugarcrm REST api but I am not able to do so as I am not even able to login with the documentation code, I tried same thing as given in documentation
<?php
// specify the REST web service to interact with
$url = 'localhost/~jmertic/sugarcrm/service/v4_1/rest.php';
// Open a curl session for making the call
$curl = curl_init($url);
// Tell curl to use HTTP POST
curl_setopt($curl, CURLOPT_POST, true);
// Tell curl not to return headers, but do return the response
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// Set the POST arguments to pass to the Sugar server
$parameters = array(
'user_auth' => array(
'user_name' => 'username',
'password' => md5('password'),
),
);
$json = json_encode($parameters);
$postArgs = array(
'method' => 'login',
'input_type' => 'JSON',
'response_type' => 'JSON',
'rest_data' => $json,
);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postArgs);
// Make the REST call, returning the result
$response = curl_exec($curl);
// Convert the result from JSON format to a PHP array
$result = json_decode($response);
if ( !is_object($result) ) {
die("Error handling result.\n");
}
if ( !isset($result->id) ) {
die("Error: {$result->name} - {$result->description}\n.");
}
// Get the session id
$sessionId = $result->id;
changed the username,password and url to match my setup but i get an error stating
No direct script access allowed
I tried to search this on web but couldnt find any relevant solution.
I am using sugarCRM 6.5.0RC2 version
Regards,
Anand Joshi
You probably has some defense configured on your WEB server which allows you to access only to index.php.
To verify it, try to go from the browser to your API URL: http://YOUR_DOMAIN_NAME/service/v4_1/rest.php
Or/and run from terminal: wget http://YOUR_DOMAIN_NAME/service/v4_1/rest.php
If it shows the same message, check your .httaccess on this folder or/and your web server config file.
If no, how do you run the API test script? through CLI or from browser?
Also I suggest you to use some Open Source SugarCRM REST API Wrapper. I use this one: https://github.com/asakusuma/SugarCRM-REST-API-Wrapper-Class
Shouldn't the line be this...
$url = 'http://yoursugarinstance/service/v4_1/rest.php';
I would like to submit my form to another page but making it not go to that page (like AJAX, but I know that AJAX does not work across domains)
Do you guys know how to do this? I don't like submitting it to the page on the other site because it is just really a slower and crappier way of doing things.
Thanks,
Nathan Johnson
Submit your form to a local page via AJAX. From that page you can post the data to the remote site with e.g. cURL.
Here's a very abstract example:
page_with_form.php
<form id="form1">
//input fields
</form>
<script>
$.post('post_to_remote.php', $('#form1').serialize(), function(){
//do something when finished
return false; //prevent from reloading
});
</script>
post_to_remote.php
$param1 = $_POST['param1'];
$param2 = $_POST['param2'];
$remoteUrl = 'http://www.remote_site.com/page_to_post_to.php';
$postFields = array('param1' => $param1, 'param2' => $param2);
//if you don't want to do any sanitizing, you can also simply do this:
//$postFields = $_POST;
$data_from_remote_page = $getUrl($remoteUrl, 'post', $postFileds);
function getUrl($url, $method='', $vars='') {
$ch = curl_init();
if ($method == 'post') {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt');
$buffer = curl_exec($ch);
curl_close($ch);
return $buffer;
}
If you do not need the full power of curl and it's really just a simple post, you can also use native PHP functions:
$postFields = http_build_query($_POST);
$remoteUrl = 'http://www.remote_site.com/page_to_post_to.php';
$context = stream_context_create(
array(
'http' => array(
'method' => 'POST',
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'content' => $postFields,
'timeout' => 10,
),
)
);
$result = file_get_contents($remoteURL, false, $context);
A vary basic example, but you get the idea.
You can try using JSONP:
http://davidwalsh.name/jsonp
http://api.jquery.com/jQuery.getJSON/#jsonp
It can be used cross-domain, but the data you send back from the server has to be something like (PHP):
echo $_GET['callback']."(".json_encode($data).")";
When I first used it I didn't echoed the callback function's name and it took me a couple of hours to see why it wasn't working.
Good luck!
Try jsonp in javascript:
$.ajax({
url: 'some_url' ,
data: $('#form_id').serialize(),
dataType: "jsonp",
jsonp : "callback",
jsonpCallback: "jsonpcallbask"
});
function jsonpcallbask(data) {
//handle response here
}
I have an application that uses the old Facebook API but now I'm migrating it. The application works good until I try to upload a photo.
I knew how to do it in the old way, but now... I'm in troubles.
This is the way I used to do it:
$args = array
(
'method' => 'photos.upload',
'v' => $ver,
'api_key' => $key,
'uid' => $uid,
'call_id' => $cid,
'format' => 'XML',
'caption' => $caption
);
signRequest($args, $sec);
$args[basename($file)] = '#' . realpath($file);
$ch = curl_init();
$url = 'http://api.facebook.com/restserver.php?method=photos.upload';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
$data = curl_exec($ch);
Any ideas??
Thanks
The API url starts with https:// not http://. That could be the issue.
I found the solution here:
Uploading a picture to facebook
There is shown how to use the new Facebook Graph API with the PHP Curl function and a valid session token.