Post request using curl not working - PHP - php

I want to get the response from a PHP post as a string. I'm using curl, based in some references I've found here.
This is my function:
function curlPost($url, $parameters){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 2);
curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
It really seems right, but when I run it, it just doesn't happen anything. I've tried hardcoding $url and $parameter, but it also didn't work.
Any ideas?
Thanks!

Related

Make PHP Request to tomcat localhost using cURL and get response

I'm new to PHP and am currently struggling to set this up. Could someone provide me an outline of how you use curl to post and retrieve the returning data?
I tried this out:
$url = 'http://localhost:8080/ds/stuff?maybe=false';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, $string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
But I keep getting the HTTP status 405 Error
(aka this):
Am I doing something wrong/what should I do? Or do I have to change something in my ds/stuff
You should set CURLOPT_POST to true. POST data goes to CURLOPT_POSTFIELDS.
$url = 'http://localhost:8080/ds/stuff?maybe=false';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);

Alternatives to file_get_contents('php://input') - cURL proposed, can't get it to work

For when web-hosts don't allow file_get_contents (either directly, or by allow_url_fopen). I want to put together a list of alternatives. Can anyone offer suggestions? I found that cURL could be used, but the code below doesn't return anything in $output.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"php://input" );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
echo $output;
File_get_contents alternative (CURL):
<?php
function file_get_contents_curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
?>
Note: not made by me, found this on the internet some weeks ago.

No response from cURL function

I have written the following function, which was code I used somewhere else and modified slightly to work as a function (using $url in function parameters):
function curl2str($url) {
$cURL = curl_init($url);
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
curl_setopt($cURL, CURLOPT_CONNECTTIMEOUT, 5);
$data = curl_exec($cURL);
curl_close($cURL);
return $data;
}
I simply want a function to return a URL into a string, for a quick and easy API. The URL I am passing it is valid and works fine when I put it into a browser. I am calling it like so:
<?=curl2str("**valid URL here**");?>
For some reason it is just returning false. What am I doing wrong?
update
When I put this questions URL into as $url, I get a response. But when I use my custom URL, which works fine in the browser, and simply display's a list of files in the directory, I get bool:false.
update 2
It would seem that any domain works fine, apart from the one that I am trying to access. It just so happens that this is a root domain on the same server, I am running this script from a subdomain, but because of basedir_restrictions I cannot access a folder from the subdomain. So I wrote a little php to get the contents of the folder, and output it to the browser as a serialized array (JSON is not installed). But I cannot get a response from this root domain at all. It works fine in the browser, just not in cURL. And everything else works fine in cURL.
:(
Try this code into your function:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
if(curl_errno($ch))
echo 'Curl error: '.curl_error($ch);
curl_close ($ch);
Note: curl_errno($ch); return error number>0 if any error occurs from cURL and use curl_error($ch); to see what is the error from cURL.
I use this function:
function curl($url, $cookie = false, $post = false, $header = false, $follow_location = false, $referer=false,$proxy=false)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_REFERER, $referer);
curl_setopt($ch, CURLOPT_HEADER, $header);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $follow_location);
if ($cookie) {
curl_setopt ($ch, CURLOPT_COOKIE, $cookie);
}
if ($post) {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
}
$response = curl_exec ($ch);
curl_close($ch);
return $response;
}

How to get JSON response using curl

Hey there I am started to learn foursquare API, but I am stuck at getting an Access Token.
Here is a part from the code I found in SO.
// build url
$url = 'https://foursquare.com/oauth2/access_token';
$url .= '?client_id='.CLIENT_ID;
$url .= '&client_secret='.CLIENT_SECRET;
$url .= '&grant_type=authorization_code';
$url .= '&redirect_uri=**********/callback'; //change to your 4sq callback
$url .= '&code='.$code;
// call to https://foursquare.com/oauth2/access_token with $code
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
$result = curl_exec($ch);
However this did not work, so I have tried to find error. First echoed $url and manually clicked on that link. It worked, foursquare has returned me an access token in json format. So the problem is in the curl part of the code.
Can you find my error? and more importantly can you suggest me some resources to study on curl?
EDIT:
I did a var_dump($result) and the output is 'boolean false'
The problem is http*s*, try adding these:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
If it is a POST a request then this is the proper way to do it :
$body.='client_id='.CLIENT_ID etc.
$c = curl_init ();
curl_setopt($c, CURLOPT_URL, $url);
curl_setopt ($c, CURLOPT_POST, true);
curl_setopt ($c, CURLOPT_POSTFIELDS, $body);
curl_setopt ($c, CURLOPT_RETURNTRANSFER, true);
$page = curl_exec ($c);
curl_close ($c);

how can I create a function that can post my parameters using curl

I developed a API for my website. API working 100% correctly. But I designed it to catch and execute functions from this kind of POST parameters$_POST['functionName']; $_POST['parameter']; Now, in request page i want to send those as $_POST, I managed to do deal with that. But my problem is, how can I create a function that can post my parameters for each functions in API using curl. Here is my code which send requests to my API.
$url = 'http://localhost/myapi/api.php';
$postfields['functionName'] = "myFunction";
$postfields['parameter'] = "1";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 100);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
$data = curl_exec($ch);
$results = json_decode($data, true);
curl_close($ch);

Categories