here's my code:
<?php
$url = 'http://localhost:2304/index.php/testproj/files/add/';
$name = "test";
$fields = array(
'name'=>urlencode($name)
);
$fields_string = "";
foreach($fields as $key=>$value) {
$fields_string .= $key.'='.$value.'&';
}
rtrim($fields_string,'&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//execute post
$result = curl_exec($ch);
var_dump($result);
//close connection
curl_close($ch);
?>
I am trying to send post data to a CodeIgniter controller. I decided to use CURL to do the job. however, it doesn't work, when I put "blah" in my controller, it doesn't return anything. When I access the URL directly, it shows "blah".
You could use my Curl library:
$this->load->library('curl');
$result = $this->curl->simple_get('http://example.com/');
var_dump($result);
Try adding this into options
$ch = curl_init();
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
Edit 1
add this
curl_setopt($ch, CURLOPT_HEADER, 1);
// and post the result of $result
Related
A dummy script that reponds with some json..
<?php
$result['code'] = 200;
echo json_encode($result);
?>
However when I make a curl call to this script,trying to fetch json response and then convert it to array..things go wrong.
Usually I do a simple json_decode($json) and it gives me an array,but apparently when I var_dump json_decode($json) , it gives me int(1) as response. I simply want to fetch the code from the json.
This is my code :
public function curlcall($username, $msg)
{
$url = 'someurl.php';
$fields = array('username' => $username, 'message' => $msg);
$fields_string = null;
foreach ($fields as $key => $value) {$fields_string .= $key . '=' . $value . '&';}
rtrim($fields_string, '&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
echo '-------------------------';
var_dump($result);
exit;
}
When I directly call the script from rest client, it gives me perfect json response. But when I request the same json via curl, I dont get the desired result while converting that json in to array.
For simplicity sake, I just echoed a string but still when I var_dump it,it gives me int(1).
Try this:
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //NOTICE this line, which says you want a response returned to the calling script
$result = curl_exec($ch);
curl_close($ch);
echo '-------------------------';
var_dump($result);
I'm trying to write some PHPUnit tests for ajax endpoints. I can do this no sweat when the user doesn't have to be logged in. But to test the endpoint in question, the user does have to be logged in and I want to get the cookie programmatically as part of the test. Basically the test I want to run looks like:
$url = "https://example.com/ajax/endpoint";
$fields = array(
'name'=>'test '.rand(),
'host'=>rand(0,255).'.'.rand(0,255).'.'.rand(0,255).'.'.rand(0,255),
'port'=>rand(1,1000)
);
$fields_string = "";
foreach ($fields as $key=>$value) {
$fields_string .= $key.'='.$value.'&';
}
$fields_string = rtrim($fields_string, '&');
ob_start();
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_COOKIE, $userCookie);
curl_setopt($ch, CURLOPT_URL, $url);
$response = curl_exec($ch);
$this->assertEquals(true, $response);
$json = ob_get_contents();
$return = json_decode($json);
$this->assertEquals('false', $return->success);
$this->assertEquals('', $return->data);
ob_end_clean();
But I don't know a good way to get $userCookie other than opening a browser, logging in, and then reading PHPSESSID from the cookies in the browser. How can I get the cookie for this without grabbing it manually? I'd like to be able to get it from a curl request to the login endpoint:
$url = "https://example.com/ajax/login";
$fields = array(
'username'=>$username,
'password'=>$password
);
$fields_string = "";
foreach ($fields as $key=>$value) {
$fields_string .= $key.'='.$value.'&';
}
$fields_string = rtrim($fields_string, '&');
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_URL, $url);
$response = curl_exec($ch);
Does anything stop you to grab it programmatically?
$ch = curl_init('https://example.com/login');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "login=root&password=toor");
curl_setopt($ch, CURLOPT_HEADER, 1);
$result = curl_exec($ch);
preg_match_all('/^Set-Cookie: PHPSESSID=(.*?);/mi', $result, $matches);
$userCookie = $matches[1];
You're halfway there with your login cURL request. What you need to do is retrieve the cookie from the response headers to get the PHPSESSID.
How to get response headers with cURL has been asked in another question you can find here.
Once you've got the response headers, you'll have to parse it to get the Set-Cookie header. Using the content of that header for your subsequent tests should work from there.
Im printing on the $url the $_REQUEST and $_POST variables.. only showing an empty array..
what am I doing wrong?
function redirect_post($url, $data, $headers = null)
{
// Url Encoding Data
$encdata = array();
foreach ($data as $key => $value) {
$encdata[$key] = urlencode($value);
}
//url-ify the data for the POST
$encdata_string = http_build_query($encdata);
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
curl_setopt($ch,CURLOPT_POST,count($encdata));
curl_setopt($ch,CURLOPT_POSTFIELDS,$encdata_string);
//execute post
$result = curl_exec($ch);
echo $result;
die;
}
Try using the following code. It looks like you need to put your URL in the curl_init and then send the $data over using the curl_setopt to send the array.
$userIp = array('userIp'=>$_SERVER['REMOTE_ADDR']);
$url = 'http://xxx.xxx.xxx.xxx/somedirectory/somescript.php';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $userIp);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
My PHP code (free.php) on http://techmentry.com/free.php is
<?php
{
//Variables to POST
$access_token = "b34480a685e7d638d9ee3e53cXXXXX";
$message = "hi";
$send_to = "existing_contacts";
//Initialize CURL data to send via POST to the API
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://freesmsgateway.com/api_send");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
array('access_token' => $access_token,
'message' => urlencode('hi'),
'send_to' => $send_to,)
);
//Execute CURL command and return into variable $result
$result = curl_exec($ch);
//Do stuff
echo "$result";
}
?>
I am getting this error: THE MESSAGE WAS BLANK
This error means: "The message field was blank or was not properly URL encoded" (as told by my SMS gateway). But as you can see that my message field isn't blank.
I believe you can't send an array to CURLOPT_POSTFIELDS, you would need to replace the line with the following
curl_setopt($ch, CURLOPT_POSTFIELDS, "access_token=".$accesstoken."&message=".urlencode('hi')."&send_to=".$send_to);
I hope this solves it
Use http_build_query():
<?php
{
$postdata = array();
//Variables to POST
$postdata['access_token'] = "b34480a685e7d638d9ee3e53cXXXXX";
$postdata['message'] = "hi";
$postdata['send_to'] = "existing_contacts";
//Initialize CURL data to send via POST to the API
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://freesmsgateway.com/api_send");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postdata) );
//Execute CURL command and return into variable $result
$result = curl_exec($ch);
//Do stuff
echo "$result";
}
?>
My code is:
<?php
extract($_POST);
$url = "http://www.domain.com/";
$fields = array(
'myparam1' => 'something',
);
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
//open connection
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
//curl_setopt($ch,CURLOPT_RETURNTRANSFER, false);
$result = curl_exec($ch);
print_r("+");
curl_close($ch);
?>
The problem is it show the response in the web browse , but i dont want to. any help would be appreciated.
You have a comment on this line:
//curl_setopt($ch,CURLOPT_RETURNTRANSFER, false);
You should enable this line and set the value to true
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
As you can see on the man page here that option is used this way:
TRUE to return the transfer as a string of the return value of
curl_exec() instead of outputting it out directly.
The option CURLOPT_RETURNTRANSFER will return the output rather than printing it.
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
CURLOPT_POSTFIELDS can take array directly and CURLOPT_RETURNTRANSFER should be true
$url = "http://www.php.net";
$fields = array(
'myparam1' => 'something'
);
// open connection
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
I think this url will help you.
Get data or Content from a URL using cURL PHP