I'm looking for a method to execute a URL from an AJAX page.
The scenario is like im creating a bidding page with 3 products. Time limit is 5 second and after finishing 5 seconds, i wanted to insert the selected product details to database (this is why we need to execute a URL) and the loop should again start from 5 ..4..3..
The issue i'm facing right now is, i can not able to execute a url from this ajax call. I tried CURL; but this is not working.
$.ajax({
url: "test.php",
context: document.body
}).done(function() {
$( this ).addClass( "done" );
});
test.php
--------
curlSession = curl_init();
// HERE I NEED TO EXECUTE THE URL
curl_setopt($curlSession, CURLOPT_URL, 'http://192.168.1.132/oxid/index.php?fnc=tobasket&aid=378442f7aa427425c741607aa3440ee8&am=1');
curl_exec($curlSession);
curl_close($curlSession);
Regards,
Tismon
You can try this and confirm if this solves your purpose:
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, 'headers that you want to pass');
curl_setopt($ch, CURLOPT_URL, 'url/that/you/want/to/call');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
Posted thinking that you need help on basic curl
For using Ajax, you should look at this post.
Related
I am trying to fetch data from a URL using cURL...this is something I would be able to do but the URL that I want to fetch data from has a section of php code within it - i.e. the URL is populated using a values generated by an earlier section of code.
I want to access the URL, take the figures out of it and then plug those figures into something else. At the minute I am hitting a bit of a roadblock as the php doesn't populate the URL...any ideas?
The code I am using is:
$url = "https://*START OF API ADDRESS*<?php echo $request_ids; ?>*END OF API ADDRESS*";
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$url);
$result=curl_exec($ch);
curl_close($ch);
//var_dump(json_decode($result, true));
$rates = json_decode($result, true);
// Save result
$rates['last_updated'] = time();
file_put_contents($file, json_encode($rates));
}
I want to get specific data from rest api after user login using cookie auth but I end up with Field 'fieldname' does not exist or this field cannot be viewed by anonymous users.
Below you can see the code I wrote in ajax and php to get data. I know my script is probably awful as I used curl request for the first time so bear with me, I want someone to tell me where I am doing wrong or better explain to me how to get and post data to server with curl and get and specific data display would be appreciated.
$('#user-profile').click(function(){
$.ajax({
type: "POST",
url: "jiraticket.php",
data: $('#login-form').serialize(),
success: function(data){
alert(data);
}
});
});
and here is the script
<?php
session_start();
$url = 'http://base-url/rest/api/2/search?jql=issuetype%20=%20Epic%20AND%20status%20in%20(%22In%20Progress%22,%20Backlog,%20%22On%20hold%22,%20%22To%20Do%22)';
$curl_session = curl_init($url);
curl_setopt($curl_session, CURLOPT_HEADER, 0);
curl_setopt($curl_session, CURLOPT_RETURNTRANSFER, true);
if(isset($_COOKIE['JSESSIONID']))
$cookie_string='JSESSIONID='.$_COOKIE['JSESSIONID'];
else
$cookie_string="";
curl_setopt($curl_session, CURLOPT_HTTPHEADER, array('Content-Type: application/json' ,'Authorization: Cookie'), array('cookie:'.$cookie_string));
$response = curl_exec($curl_session);
curl_close($curl_session);
if ( !$response ) {
die('Nothing was returned.');
}
$result = json_decode($response, true);
print_r($result);
$response printing "errorMessages":["Field 'issuetype' does not exist
or this field cannot be viewed by anonymous users."]
Well, it means the API does not want to give you the value of issuetype.
Might be a typo or something, but that's not an error with your curl call, it's a problem with the API provider
I'm trying to make a registration system where users that visit us and register to our website will have an account on a secondary website in order for them to be able to see some opportunities.
So here it goes: The website were I want to POST the Form and Register
My code so far
<?php
// set post fields
$post = [
// this i don't master fell free to approach the subject for future reffrance
'authenticity_token'=>'vhmPffEAIiIbjo36K8CI77+YDQfMPBWEG8ymplsGJ0w=',
'user[email]'=>'test#gmail.com',
'user[first_name]'=>'test',
'user[last_name]'=>'test',
'user[password]'=>'12345678',//smart password (had to be 8char long)
'user[country]'=>'Country(placeholder)',
'user[mc]'=>'1560', //id of the country
'user[lc_input]'=>'1475', //id of the dropdown
'user[lc]'=>'1475',//id of the dropdown
'commit'=>'REGISTER',//value of submit
];
$ch = curl_init('https://auth.aiesec.org/users/sign_in');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
// execute!
$response = curl_exec($ch);
// close the connection, release resources used
curl_close($ch);
// do anything you want with your response
var_dump($response);
?>
Story:
As a user I want to be able to complete a form on domain A and at the same time have an account on domain B using CURL or even JS and AJAX.
If you have any solution to achieve this please let me know :) You can also have a look on the website.
Thank you!
Late edit
I`ve made this in POSTMAN. So I think I should be able to make the for post that information. If you have other suggestions.
In this case server returns 500 as success and 200 as error (security)
Final Working Code
<?php
// set post fields
$post = [
// this i don`t master fell free to approach the subject for future reffrance
'authenticity_token'=>'NKWAg8mGA9gUueBaJ5Og+oEOC5O2rZarZzMK+GnjuCA=',
'user[email]'=>'',
'user[first_name]'=>'',
'user[last_name]'=>'',
'user[password]'=>'',//smart password (had to be 8char long)
'user[country]'=>'',
'user[mc]'=>'', //id of the country
'user[lc_input]'=>'', //id of the dropdown
'user[lc]'=>'',//id of the dropdown
'commit'=>'REGISTER',//value of submit
];
$ch = curl_init('https://auth.aiesec.org/users/');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
// execute!
$response = curl_exec($ch);
// close the connection, release resources used
curl_close($ch);
// do anything you want with your response
var_dump($response);
?>
You are connection via https. So you should add these lines:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
I tested your code and the website responded "Wrong username / password". That means curl is connecting correctly.
I am trying to make an ajax call to a php script. The php script calls an rss feed using curl, gets the data, and returns the data to the funciton.
I keep getting an error
"Warning: Wrong parameter count for curl_error() in" ....
Here is my php code:1
$ch = curl_init() or die(curl_error());
curl_setopt($ch, CURLOPT_URL, $feed);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data1 = curl_exec($ch) or die(curl_error());
echo $data1;
and the ajax call:
$.ajax({
url: "getSingleFeed.php",
type: "POST",
data: "feedURL=" + window.feedURL,
success: function(feed){
alert(feed);
}});
I tested all the variables, they are being passed correctly, I can echo them out. But this line: $data1 = curl_exec($ch) or die(curl_error());
is what is giving me the error. I am doing the same thing with curl on other pages, just without ajax, and it is working fine. Is there anything special I need to do with ajax to do this?
The PHP manual states that the first parameter of curl_error is a handle returned by curl_init();. Try replacing what you pasted with what is given below. This adds the $ch variable as a parameter to all the instances of curl_error();.
$ch = curl_init() or die(curl_error($ch));
curl_setopt($ch, CURLOPT_URL, $feed);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data1 = curl_exec($ch) or die(curl_error($ch));
echo $data1;
I'm trying to fumble my way through parsing rss sensibly, using jQuery and jFeed.
Because of the same origin policy I'm pulling the BBC's health news feed into a local page (http://www.davidrhysthomas.co.uk/play/proxy.php).
Originally this was just the same proxy.php script as available in the jFeed download package, but due to my host's disabling allow_url_fopen() I've amended the php to the following:
$url = "http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/health/rss.xml";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
echo "$data";
curl_close($ch);
Which seems to generate the same/comparable contents as the original fopen on my local machine.
Now that seems to be working, I'm looking at setting the jFeed script up to work with the page and, to my embarrassment, don't see how.
I understand that, at the least, this should work:
jQuery.getFeed({
url: 'http://www.davidrhysthomas.co.uk/play/proxy.php',
success: function(feed) {
alert(feed.title);
}
});
...but, as I'm sure you anticipate, it doesn't. What non-output there is, is available for your perusal here: http://www.davidrhysthomas.co.uk/play/exampleTest.html. And I honestly don't have a clue what to do about it.
If anyone could offer some pointers, tips, hints, or, at a pinch, a quick slap around the cheeks and a 'pull yourself together!' it'd be much appreciated...
Thanks in advance =)
On your test page, you have some lines of script that look like wrong...
<script type="text/javascript">
jQuery(function() {
url: 'http://www.davidrhysthomas.co.uk/play/proxy.php',
success: function(feed) {
alert(feed.title);
}
...
I think that should be more like...
<script type="text/javascript">
jQuery(function() {
jQury.ajax( {
url: 'http://www.davidrhysthomas.co.uk/play/proxy.php',
success: function(feed) {
alert(feed.title);
}
});
...
The Zend Framework has a class for consuming all kinds of feeds.
it's called Zend_Feed
http://framework.zend.com/manual/en/zend.feed.html
In your PHP code, you missed the xml header :
$url = "http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/health/rss.xml";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
header("content-type: text/xml");
echo "$data";
curl_close($ch);