I have a function make_curl_request to make curl request.
/**
* General Function to make curl request */
function make_curl_request($url, $data)
{
ob_start();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_exec($ch);
curl_close($ch);
$strCurlResponse = ob_get_contents();
ob_end_clean();
return $strCurlResponse;
}
I am calling it like:
$strGatewayResponse = make_curl_request( REQUEST_URL, compact('strMobileNo', 'strKeywords', 'strApiKey') );
I tried the things but can't get my code working fine. Currently its just return string("") as the output. Where am i going wrong?
My target is to simple post few data to next page located on other domain and get its xml response and parse it and display it. Is there any other simple and good solution?
The problem is that you've got RETURNTRANSFER set to TRUE, which means curl returns its output instead of directly outputting it. However, you're not capturing that output in a variable, so it's dropping on the floor.
You've got two options
a) remove the ob_*() functions to remove the PHP buffering and then do
$data = curl_exec($ch)
if ($data === FALSE) {
die("Curl failed: " . curL_error($ch));
}
after which $data contains the contents of the URL you've fetched.
b) remove the RETURNTRANSFER option, and let curl do its normal "output to client directly" thing, which then gets captured by the PHP output buffering.
Try by adding a row:
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
FALSE to stop cURL from verifying the peer's certificate. Alternate certificates to verify against can be specified with the CURLOPT_CAINFO option or a certificate directory can be specified with the CURLOPT_CAPATH option.
it's not ok to send a curl POST without a header.
please find the following link. it may help
OAuth, PHP, Rest API and curl gives 400 Bad Request
Related
I want to add the HTTP headers for authenticating Udemy API access.Can someone tell me as to how to add the headers.I already have the client id and secret key.I want to access the API from a PHP page.
https://developers.udemy.com/
Here is the code i tried using:
$ch = curl_init($request);
curl_setopt($ch, CURLOPT_URL, $request);
curl_setopt($ch,CURLOPT_HTTPHEADER,array('X-Udemy-Client-Id:MY_ID','X-Udemy-Client-Secret:Secret'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$results= curl_exec($ch);
echo $results;
Output:
Blank Page
Can someone point out what the problem might be?
As #drmarvelous wrote, you perform two requests (1st - by CURL, and 2nd - by file_get_contents) which does the same. Wherein the result of CURL request is not actually used in your script. It use the result of file_get_contents request which is performed without authentication parameters. Because of this you getting Unauthorized error.
So you have to use the result of CURL request:
...
$json = json_decode($results, true);
print_r($json);
Update:
You have to ensure you use valid URL for API request, i.e. value of $request in your code should be valid URL. Also, ensure you pass valid authentication parameters (Client-Id and Client-Secret) by HTTP headers.
Furthermore, since API is secured, you have to disable SSL peer verification by setting CURLOPT_SSL_VERIFYPEER option to false.
So the code should look like this:
$ch = curl_init($request);
curl_setopt($ch, CURLOPT_URL, $request);
curl_setopt($ch,CURLOPT_HTTPHEADER,array('X-Udemy-Client-Id: {YourID}','X-Udemy-Client-Secret: {YourSecret}'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$results= curl_exec($ch);
echo $results;
I am trying to use PHP's curl() function and for some reason my code does not return any data.
I am making a request to a URL that is unverified:
Here is my code:
<?php
$ch = curl_init("**SENSITIVE URL**");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
print_r($result);
curl_close($ch);
?>
If I put in www.google.com it does return the google webpage to my site. I apoligize, but I can't give out the URL for my site but I assure you that directly going to the URL does return data.
You need to tell cURL to ignore the (bad) SSL cert. Try adding the following options:
// Do not verify the cert
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Ignore the "does not match target host name" error
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
Hi I am new to php and want to know some alternate function for the header('location:mysit.php');
I am in a scenario that I am sending the request like this:
header('Location: http://localhost/(some external site).php'&?var='test')
something like this but what I wanna do is that I want to send values of variables to the external site but I actually dont want that page to pop out.
I mean variables should be sent to some external site/page but on screen I want to be redirected to my login page. But seemingly I dont know any alternative please guide me. Thx.
You are searching for PHP cUrl:
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);
// grab URL and pass it to the browser
curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
Set the location header to the place you actually want to redirect the browser to and use something like cURL to make an HTTP request to the remote site.
The way you usually would do that is by sending those parameters by cURL, parse the return values and use them however you need.
By using cURL you can pass POST and GET variables to any URL.
Like so:
$ch = curl_init('http://example.org/?aVariable=theValue');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
Now, in $result you have the response from the URL passed to curl_init().
If you need to post data, the code needs a little more:
$ch = curl_init('http://example.org/page_to_post_to.php');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'variable1=value1&variable2=value2');
$result = curl_exec($ch);
curl_close($ch);
Again, the result from your POST reqeust is saved to $result.
You could connect to another URL in the background in numerous ways. There's cURL ( http://php.net/curl - already mentioned here in previous comments ), there's fopen ( http://php.net/manual/en/function.fopen.php ), there's fsockopen ( http://php.net/manual/en/function.fsockopen.php - little more advanced )
I want to read a server's reply for a certain request, modify it to my needs, and send it to the site visitor. get_headers() works perfectly for the headers, but if the requested file is missing (404), and that's exactly what I want to use, get_file_contents(), readfile() and other functions I've tried all break with the warning/error that the file is missing instead of reading the replied stream into a variable.
So what I want is a function similar to get_headers() only for the rest of the data, like a get_data() that doesn't cancel. Is there such a thing?
Thanks for reading.
Use curl_exec. It will always return the body unless the CURLOPT_FAILONERROR option is set to TRUE.
Here's an example:
$url = 'http://www.example.com/thisrequestwillerror';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
// This is the default, but just making sure...
curl_setopt($curl, CURLOPT_FAILONERROR, false);
// Execute and return as a string
$str = curl_exec($curl);
curl_close($curl);
// Dump the response body
var_dump($str);
Wrap this in a function and use it wherever you need to get an HTTP response body in your application.
I use the following command in some old scripts:
curl -Lk "https:www.example.com/stuff/api.php?"
I then record the header into a variable and make comparisons and so forth. What I would really like to do is convert the process to PHP. I have enabled curl, openssl, and believe I have everything ready.
What I cannot seem to find is a handy translation to convert that command line syntax to the equivalent commands in PHP.
I suspect something in the order of :
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
// What goes here so that I just get the Location and nothing else?
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Get the response and close the channel.
$response = curl_exec($ch);
curl_close($ch);
The goal being $response = the data from the api “OK=1&ect”
Thank you
I'm a little confused by your comment:
// What goes here so that I just get the Location and nothing else?
Anyway, if you want to obtain the response body from the remote server, use:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
If you want to get the headers in the response (i.e.: what your comment might be referring to):
curl_setopt($ch, CURLOPT_HEADER, 1);
If your problem is that there is a redirection between the initial call and the response, use:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);