cleaning a post URL from an Array - php

Here is my code :
$query = "SELECT first_name, surname, email FROM app2";
$result = mysql_query($query) or die(mysql_error());
$url = "https://test.com?"; // Where you want to post data
while($row = mysql_fetch_array($result)) {
$input = "";
foreach($row as $key => $value) $input .= $key . '=' . urlencode($value) . '&';
$input = rtrim($input, '& ');
$ch = curl_init(); // Initiate cURL
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true); // Tell cURL you want to post something
curl_setopt($ch, CURLOPT_POSTFIELDS, $input); // Define what you want to post
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return the output in string format
$output = curl_exec ($ch); // Execute
curl_close ($ch); // Close cURL handle
var_dump($output); // Show output
}
The problem is the data ($input) come out like this :
0=Lucky&first_name=Lucky&1=Dube&surname=Dube
It should be like this :
first_name=Lucky&surname=Dube

No need to build your query on your own. First off, just use _assoc()* function flavor, then use http_build_query on that row array batch, then feed it. It will construct the query string for you. No need to append each element with & with your foreach. Rough example:
$url = "https://test.com"; // Where you want to post data
while($row = mysql_fetch_assoc($result)) {
$input = http_build_query($row);
$ch = curl_init(); // Initiate cURL
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true); // Tell cURL you want to post something
curl_setopt($ch, CURLOPT_POSTFIELDS, $input); // Define what you want to post
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return the output in string format
$output = curl_exec ($ch); // Execute
curl_close ($ch); // Close cURL handle
}

Try changing
while($row = mysql_fetch_array($result)) {
with
while($row = mysql_fetch_assoc($result)) {
mysql_fetch_assoc returns only the associative array.

Related

How i can get input value with php curl?

İ want login instagram with php curl. When I try to login below code is always changing So i must get this input value:
<input type="hidden" name="csrfmiddlewaretoken" value="uCbmYHcQLe18VAvVFXbfyPcS5kXtCuuE">
my curl code is:
<?php
$url = "https://www.instagram.com/accounts/login/?force_classic_login";
$values = "csrfmiddlewaretoken=" + $key + "&username=myusername&password=mypassword";
$key = "";
//foreach input name csrfmiddlewaretoken = $key
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $values);
$veri = curl_exec($curl);
curl_close($curl);
echo $veri;
?>
Try this:
$veri = curl_exec($curl); // That $veri contains the curl response into it
$data = json_decode($veri, true); // Convert the response into an array
now the $data is an array and you can access the values from this array by using its indexes.
Man, explode the response content with the separator <input then get on array result your data.
Do something like this:
$result = explode('<input type="hidden" name="csrfmiddlewaretoken" value="', $file_content);
$result = explode('">', $result[1] );
$input_value = $result[0];
$input_value = $result[0];
i hope this help

Curl doesn't update database

I am trying to update my online database with the offline version. The following code executes without giving an error code but the expected result is not showing. Any help will be really appreciated as series of Google searching proved abortive.
<?php
include("inc_files/inventory.php");
// update sales_rec records
$get_sales_rec = mysql_query("SELECT * FROM sales_rec WHERE status = '0' ORDER BY id ASC") or die(mysql_error());
while($rec = mysql_fetch_array($get_sales_rec)){
$id = $rec['id'];
$receipt = $rec['receipt'];
$customer = $rec['customer'];
$total = $rec['total'];
$amount = $rec['amount'];
$balance = $rec['balance'];
$discount = $rec['discount'];
$date = $rec['date'];
$time = $rec['time'];
? $type = "sales_rec";
$ch = curl_init(); // initiate curl
$url = "http://www.sample.com/inventory/test.php"; // where you want to post data
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, true); // tell curl you want to post something
curl_setopt($ch, CURLOPT_POSTFIELDS, "type=$type&r=$receipt&c=$customer&t=$total&a=$amount&b=$balance&d=$discount&dt=$date&ti=$time"); // define what you want to post
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return the output in string format
$output = curl_exec ($ch); // execute
curl_close ($ch); // close curl handle
}
?>
have you tried passing an array
$data = array('name' => 'aaa', 'title' => 'dev');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

CURL not passing post parameters

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);

How to assign result fro curl to a variable?

I need to do a simple POST request and parse the result. For this i use curl in php. The problem is that i cant assign the result to a variable - it just prints .
My method:
private function sendRequest($data)
{
$ch = curl_init(self::IP . ':' . self::PORT);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch); // HERE IT PRINTS
curl_close($ch);
$parsed_result = #simplexml_load_string(trim($result)); // OR HERE IT
die(var_dump(isset($parsed_result->request_error)));
if (isset($parsed_result->request_error))
$this->AJAXResult(TRUE, (string) $parsed_result->request_error->text);
}
You must add:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1)
if you want curl_exec to return the result of the post request.

php youtube json decode

I tried to use JSON decode to get the youtube API feed. However, when I paste the JSON result in http://www.jsonlint.com/ I noticed something like
"media$group": {
"media$category": [
Unfortunately some symbols are rejected by php. Here is my code, I tried to remove this $ symbol, but maybe not success. How do I solve this?
$url = 'http://gdata.youtube.com/feeds/api/videosq=football&orderby=published&v=2&alt=json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $url);
$body1 = curl_exec($ch);
$body = str_replace('$','', $body1);
curl_close($ch);
$data = json_decode($body);
foreach ($data->feed->entry as $result) {
...
}
Your problem is the usage of PHP identifiers to access the contents. The simplest solution here would be to get an array instead of an object:
$data = json_decode ( $json , $assoc = true );
This allows access to fields with:
echo $result['media$group']['media$description'];
If you want to keep the object syntax, that's possible with this kludge:
echo $result->{'media$group'}->{'media$category'};
(But arrays are safer here. You don't get a fatal error should the format change and properties be absent.)
This work:
<?php
$url = 'http://gdata.youtube.com/feeds/api/videos?q=football&orderby=published&v=2&alt=json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $url);
$body1 = curl_exec($ch);
$body = str_replace('$','', $body1);
curl_close($ch);
$data = json_decode($body);
foreach ($data->feed->entry as $result) {
var_dump($result);
}
?>

Categories