Sending additional coockie variables with failure - php

I bother with sending additional coockies to server. I'm receiving coockies from server uses to connecting with success but when i try to add more data in coockie, server do not receive it. I tried few ways to fix it but now im tired and devoid of ideas. Looking forward for your any helpful replies!
Here is the code:
$cookie = 'var1='.urlencode($config['log']);
curl_setopt($ch, CURLOPT_COOKIE, $cookie); curl_setopt($ch, CURLOPT_URL, 'https://locaIp.adress/'); curl_setopt($ch, CURLOPT_POSTFIELDS, array('var1' => $config['log'],); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $echo = curl_exec($ch); </i>

HttpOnly was my initial pick, but you said that you post one var, but no multiple (I'm guessing that's what you mean adding more data). If that's the issue it should be preparing the data before you send it.
$cookie = 'log1='.urlencode($config2['log1']).";".'log2='.urlencode($config2['log2']);
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIE, $cookie ); // it works like this
//curl_setopt($ch, CURLOPT_COOKIE, "log1=cookie1;log2=cookie2"); // or like this

Related

PHP CURL POST data with value empty

I have this code to POST data to an url that is receiving the data with below, with content-type header set as text/html
file_get_contents("php://input");
This is the code That I used to POST to url and it is sending the data, but without values (i'm sending an array data with key values).
$url = "http://url im sending data to";
$object = array(
"key1" => "123",
"key2" => "345",
"key3" => "567"
);
$data = http_build_query($object, '', '&');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTREDIR, 3);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
However, it's sending the data, but without the values, since I get a response back from the url saying that values are empty.
Moreover, I checked the curl_errno($ch) and it doesn't return anything so there is no error within my code (I think?)
Can someone help me out?!
Thanks in advance!
OK I solved the problem and it was pretty simple.
Just swap the http_build_query with json_encode, and make sure to only set the curl_setopt as it is.
add the bottom code also.
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
I don't know why but for some reason, if I add other curl_setopt variables like
curl_setopt($ch, CURLOPT_POST, true);
it does not work.

JIRA Update custom fields using REST, PHP and cURL

I am trying to update some custom fields using the REST API and PHP/cURL.
I'm wondering if I might have edited something without realizing it, while what I have below "worked" yesterday (I think), it does not work now.
I get varying responses using the different "methods", from:
I get this one using the POST method, as it is uncommented below.
HTTP 405 - The specified HTTP method is not allowed for the requested
resource ().
I get this one if I use the commented-out PUT method, with POST commented out.
{"status-code":500,"message":"Read timed out"}
And this one mixing and matching PUT and POST.
{"errorMessages":["No content to map to Object due to end of input"]}
What am I missing/doing wrong? I am using the following code:
<?php
$username = 'username';
$password = 'password';
$url = "https://example.com/rest/api/2/issue/PROJ-827";
$ch = curl_init();
$headers = array(
'Accept: application/json',
'Content-Type: application/json'
);
$test = "This is the content of the custom field.";
$data = <<<JSON
{
"fields": {
"customfield_11334" : ["$test"]
}
}
JSON;
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
// Also tried, with the above two lines commented out...
// curl_setopt($ch, CURLOPT_PUT, 1);
// curl_setopt($ch, CURLOPT_INFILE, $data);
// curl_setopt($ch, CURLOPT_INFILESIZE, strlen($data));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
$result = curl_exec($ch);
$ch_error = curl_error($ch);
if ($ch_error) {
echo "cURL Error: $ch_error";
} else {
echo $result;
}
curl_close($ch);
?>
The problem here is that PHP's cURL API is not particularly intuitive.
You might think that because a POST request body is sent using the following option
that a PUT request would be done the same way:
// works for sending a POST request
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
// DOES NOT work to send a PUT request
curl_setopt($ch, CURLOPT_PUT, 1);
curl_setopt($ch, CURLOPT_PUTFIELDS, $data);
Instead, to send a PUT request (with associated body data), you need the following:
// The correct way to send a PUT request
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
Note that even though you're sending a PUT request, you still have to use the CURLOPT_POSTFIELDS
option to send your PUT request body. It's a confusing and inconsistent process, but it's what you've
got if you want to use the PHP cURL bindings.
According to the relevant manual entrydocs, the CURLOPT_PUT option seems to only work for PUTting a file directly:
TRUE to HTTP PUT a file. The file to PUT must be set with CURLOPT_INFILE and CURLOPT_INFILESIZE.
A better option IMHO is to use a custom stream wrapper for HTTP client operations. This carries the
added benefit of not making your application reliant on the underlying libcurl library. Such an
implementation is beyond the scope of this question, though. Google is your friend if you're interested
in developing a stream wrapper solution.

CURLOPT_COOKIEFILE doesnt append Cookie header

Im using MAMP on a Mac running OS X Lion.
I need to connect to a remote site sending the cookie.
All goes well except for the cookie part.
For the cookie part I'm using this code:
$cookieFile = dirname(__FILE__).'/cookie.txt';
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFile);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieFile);
The CURLOPT_COOKIEJAR part does create a cookie, but on a subsequent request CURLOPT_COOKIEFILE doesn't add a cookie header. I've checked this using HTTPScoop (a Fiddler like tool).
Any idea what might be the problem?
EDIT:
Im connecting to a ASP.Net site. Problem seems to be the im not getting a ASP.NET_SessionId cookie. The cookie i do get has a key without a value, thats probably the reason why it isn't posted.
Any idea how to force the server to send a session cookie?
We'd really need to see more code, but here is a sample bit I have which collects a session cookie from an initial request then uses it in a subsequent POST. It uses an anonymous proxy to run a GET request on an arbitrary URL, hopefully it helps you (to be clear though it doesn't use the COOKIEJAR, but I feel it may still be helpful).
<?php
define('TARGET_URL', 'http://moxune.com');
echo 'Sending initial request' . PHP_EOL;
$aHeaders = get_headers("http://420proxy.info");
foreach($aHeaders as $sHeader) {
if(stripos($sHeader, 'set-cookie') !== false) {
// extract the cookie from the first response
$aCookie = explode(':', $sHeader);
$sCookie = trim(array_pop($aCookie));
$oCookie = http_parse_cookie($sCookie);
echo 'Cookie extracted, trying to POST now' . PHP_EOL;
// OK, now let's try the POST request
$ch = curl_init('http://420proxy.info/includes/process.php?action=update');
curl_setopt($ch, CURLOPT_REFERER, '420.proxy.info');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_COOKIE, $sCookie);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect: 100-continue'));
//curl_setopt($ch, CURLOPT_COOKIE, http_build_cookie((array)$oCookie));
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
'u' => TARGET_URL,
'allowCookies' => 'off',
'encodeURL' => 'off',
'stripJS' => 'on'
)
);
$response = curl_exec($ch);
die(var_dump($response));
}
}

getting results from a site using curl

I have been trying to post some variables to a site using POST method, using curl to get some results. I am posting to this link.
http://www.rasta.pk/Lhr/Lhr_Traffic.aspx
At this page you will see a drop down menu .. onchange some values are returned in "Yellow" colored table.
I have monitored this site and trying to get those results by making a post request to that link. But, I am getting "Bad Header" error. I have tried changing things but ubable to find a solution.
Here is my code:
"Canal Bank Rd",
"ScriptManager1 " => "UpdatePanelDDLRoads|DropDownRoads",
"__EVENTARGUMENT" => "",
"__EVENTTARGET" => "DropDownRoads"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headerz);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_REFERER, "http://www.rasta.pk/Lhr/Lhr_Traffic.aspx");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
echo $output;
?>
There's too little details presented for us to know for sure.
A guess is that you're doing the wrong kind of post, since when you pass in a hash array to CURLOPT_POSTFIELDS it will do a multipart formpost which might not be what the server expects. Pass in a string instead to make a "normal" POST.
If that is not enough, use LiveHTTPHeaders or similar in a browser to figure out exactly what is sent in a "manual" session and then you make sure that your curl program mimics that operation as closely as possible.

My password curl is failing sometimes?

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $PathUrl);
curl_setopt($ch, CURLOPT_USERPWD, 'someuser:somepass');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
$info = curl_getinfo($ch);
Any ideas on why it works about 30% of the time and the other 70% if fails....viewing the url on any browser works all the time
You may be better off setting the Authorization header via CURLOPT_HTTPHEADER.
Eg, curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization' => 'user:pass'))
Edit: also, this may not apply because you say it works 30% of the time, but just be aware of common forms of encoding for Auth headers, eg, base64.

Categories