I'm trying to figure out how to use the Cheddar API (http://cheddarapp.com/developer) in my PHP application.
Cheddar's API uses curl requests - which have been fine for me using in terminal but not in my index.php.
I'd like to create a button that when clicked, creates a task in a list call Colors. If a list does not exist, it'll create the list.
Have anybody used Cheddar's API or even included curl requests in PHP or even how to include them in Javascript which I'm guessing you use for things of this matter.
Update
Here's the Curl request for creating a task in Cheddar: https://cheddarapp.com/developer/tasks#create.
I'd like to make a button that onclick, it will create a task. Is it not as a simple as creating a function in Javascript and using onclick on an anchor?
I am using now days curl Php
$LOCAL_REST_URL = 'whateverurlofyourrestapi'
$json_part = { pass the data for post }
you will get your response in $buffer in json format iterate it use the response
As u said how to make a curl request i would like to give you a simple POST example
$curl_handle=curl_init();
curl_setopt($curl_handle,CURLOPT_URL,$LOCAL_REST_URL);
curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,20);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS,$json_part);
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);
Where $json_partis the request body and $LOCAL_REST_URL is is your rest url
I am hoping this post will help you
You could run your terminal program with shell_exec from php.
Or transcript the curl-code to curl-requests from php, http://se2.php.net/manual/en/ref.curl.php
Related
I have been asked to deal with web hook concept. I am very new to this concept and i will need your help. I was asked to provide a URL to a company so they can send json data from their website.
So far i found this :
// Initiate curl
$ch = curl_init();
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL,$url);
// Execute
$result=curl_exec($ch);
// Closing
curl_close($ch);
// Will dump a beauty json :3
var_dump(json_decode($result, true));
so is URL same as this PHP file? I am really confused. Thank You
Update: I have created an article to help with this
You don't need to use curl for a HubSpot webhook, the webhook should trigger some kind of action on your server and receive posted data ready for massaging and manipulating. A webhook could be, when a contact fills out a form or completes a specific action within HubSpot and you push that data to your custom script on a stand alone server. You should start with how to handle JSON data in a POST request. http://edwin.baculsoft.com/2011/12/how-to-handle-json-post-request-using-php/
That article should help out a bit...
Since you don't normally know what that posted data from HubSpot looks like just yet, you can use a service like requestbin to view the data that is being posted.. Note: they might have a cap on the amount of JSON it can handle in the request so you might need to find another service that gives you the complete JSON data or create a script on your server that saves the request to a file. For requestbin, you would create a workflow in hubspot with the action being a webhook and set the type to post and the url to the url that you received when creating a new requestbin.
Once you know what the JSON data structure is that is coming into your server, you can start to go to work on manipulating that data.
It's hard to be sure from the info you are providing, but you are probably coming at this the wrong way around.
You probably need to write a php script that will be the target of a webhook. Here is a post that should get you started:
Catching json data from hubspot webhook
I'm currently using an API which returns a JSON object. I pay per hit, so I would like to minimize my hits. I use this object to fill in images and text on my page. The object that gets returned is very similar to a itunes lookup hit.
A simplified version of my code is this:
<img id="test" src="" alt="Image" />
<script>
$.getJSON( "https://itunes.apple.com/lookup?id=284910350", function( data ) {
document.getElementById('test').setAttribute("src", data.results[0].screenshotUrls[0]);
});
</script>
Everytime a users opens this page, a request gets sent to the server and a hit gets added to my account (obviously). I would like to store the object temporarily on my own server so I can request the data once, and serve a 'local' version to the user. What is the best way to do this? Is it possible to have the file updated every week or so automatically?
Thanks in advance!
It's an easy cron job. Assuming that you can execute bash script in your server:
1 - In your server put a bash script called fetchItune.sh. The content of this script basically stores some curl requests to outside API:
#!/bin/sh
curl -H "Accept: application/json" https://itunes.apple.com/lookup\?id\=284910350 -o /path/to/storage/data.json
You can get fancy with this script e.g. putting the list of endpoints in an array or output to different files, etc. but at the core, just make sure they are valid HTTP requests that accept a JSON response.
2 - Set up a cron job to do it weekly. It could be as simple as putting this script in /etc/cron.weekly if you are using an Ubuntu server. Otherwise, please search through your server documentation. I'm sure there is a section on cron job.
3 - From your JavaScript, request your server endpoint instead of the outside API:
<script>
$.getJSON( "/path/to/storage/data.json", function( data ) {
document.getElementById('test').setAttribute("src", data.results[0].screenshotUrls[0]);
});
</script>
EDIT: You can write PHP script to make request to external API instead of bash script. The principle is the same. I take this directly from PHP curl documentation: http://php.net/manual/en/curl.examples-basic.php
<?php
$ch = curl_init("https://itunes.apple.com/lookup\?id\=284910350");
$fp = fopen("/path/to/storage/data.json", "w");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
curl_close($ch);
fclose($fp);
?>
I will set up a register page using MSSQL.
The system must work like:
User appends data at something.com/register.php
The data is sent to host-ip-address/regsecond.php which my database will be at. (For security reasons, this php page wont directly access to the database.
The php page at host will start another PHP page or EXE file will directly reach database directly and securely.
As my php level is not high, I wanted to learn If i could start php scripts which will work and do their job without coming into users browsers. Here I explain what I say:
" I append some data at x.php, and it starts another PHP script which will do the job with the DATA appended from x.php but the -another PHP script- wont come into users browser "
I was hopefully clear ,as summary, should I use exe [will be harder] or can I start PHP script without coming into browser. And how of course.
You can do this using the curl extension. You can find info on it here:
http://php.net/manual/en/book.curl.php
You can do something like the following:
$postdata = array(
'item1' => 'data'
);
$ch = curl_init("http://host-ip-address/regsecond.php");
curl_setopt ($ch, CURLOPT_POST, true);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);
curl_exec($ch);
curl_close($ch);
This makes a call directly from your first script to your second script without exposing anything to the user. On the far side, the data will come in as regular post data ($_POST).
You can't post data through PHP to a different website.
If you would like your website then you can configure your PHP script to connect to a different server for your MySQL, I wouldn't say it's a huge amount safer. For example
Instead of:
mysql_connect(localhost,username,password);
Try this
mysql_connect(http://your-ip:portnumber,username,password);
I'm not sure I understand this correctly but you may
§1 use a "public" php script that invokes a private one:
<?php
//public register script
//now call private
//store data to txt-file or similar..
require('/path/outside/www-data/script_that_processes_further.php');
§2 request a script at another server,
<?php
file_get_contents('http://asdf.aspx?firstname=' . $theFirstName); //simplistic
//other options would be curl, xml/soap or whatever.
§1 may be used with §2.
regards,
/t
I am a green programmer and I was originally trying to make cross domain requests in JS. I quickly learned that this is not allowed. Unlike similar questions posted on here, I would like to see if I can use PHP to make them for me instead of JSONP requests. Is this possible?
Simple workflow...
BROWSER: POST to my PHP the request-payload & request-headers
PHP: POST to Other Domain's URL the request-payload & request-headers
Other Domain: Process Request and send response
PHP: Send the Response-Content and Response-Header Info back to the browser
Here is what I am trying to work with http://msdn.microsoft.com/en-us/library/bb969500%28v=office.12%29.aspx
My goal is to make a Communicator Web Access Client that is web based and mobile friendly.
A link to a working example would be awesome!
CURL yould be your option in this case, something simple as:
<?php
$ch = curl_init('http://otherdomain.com/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
$result = curl_exec($ch);
var_dump($result);
?>
In this case, $result would contain the html code of the site. Please be aware that it doesn't going to execute any javascript as if you were visiting the site on the browser.
You are talking about web services and seems that the goal is process payments. Any major payment gateway have APIs prepared for that. In any case you can study by your own. Here a good starting point http://ajaxonomy.com/2008/xml/web-services-part-1-soap-vs-rest
I have a form on my site which sends data to some remote site - simple html form.
What I want to do is to use data user enters into form for statistical purposes.
So I instead of sending data to the remote page I send it first to my script which resends it the remote site.
The thing is I need it to behave in exact way the usual form would behave taking user to the remote site and displaying resources.
When I use this code it kinda works but not in the way I want it to:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $action);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$result = curl_exec($ch);
curl_close($ch);
Problem is that it displays response in the same script. For example if $action is for example:
somesite.com/processform.php and my script name is mysqcript.php it would display the response of "somesite.com/processform.php" inside "mysqcript.php" so all the relative links are not working.
How do I make it to send the user to "somesite.com/processform.php"? Same thing that pressing the button would do?
Leonti
I think you will have to do this on your end, as translating relative paths is the client's job. It should be simple: Just take the base directory of the request you made
http://otherdomain.com/my/request/path.php
and add it in front of every outgoing link that does not begin with "/" or a protocol ("http://", "ftp://").
Detecting all the outgoing links is hard, but I am 100% sure there are ready-made PHP classes that do that. Check for example this article and the getLinks() function in the user comments. I am not 100% sure whether this is what you need but it certainly goes to the right direction.
Here are a couple of possible solutions, which I post separately so they don't get mixed up with the one I recommend:
1 - keep using cURL, parse the response and add a <base/> tag to it. It should work for pretty much everything on that page.
<base href="http://realsite.com/form_url.php" />
2 - do not alter the submit URL. Submit the form to the real URL, but capture its content using some Javascript library (YUI does that) and send it to your script via XHR. It's still kind of hacky though.
There are several ways to do that. Here's one of the easiest: just use a 307 redirect.
header('Location: http://realsite.com/form_url.php', true, 307');
You can do your logging and stuff either before or after header() but if you do it after calling header() you will need to start your script with
ignore_user_abort(true);
Note that browsers are supposed to notify the user that their form is being redirected.