Send POST with cURL postfields to iframe - php

I am trying to send POST data using CURL to an url. This URL is called by an iframe, which is in my application.
<iframe frameborder="0" width="950" height="1000" src="/tools/costs"></iframe>
public function costs()
{
$url = "http://someurl.com/tools/costs";
if($_POST)
{
$fields_str = http_build_query($_POST);
$query = preg_replace('/%5B[0-9]+%5D/simU', '%5B%5D',$fields_str);
$realstring = str_replace("%5D", "", str_replace("%5B", "", $query));
// $realstring echo:
var_carrier=3&var_carrier=5&var_carrier=6&var_service=Demurrage+%2F+Storage&var_service=Detention&var_pod=Ancona&var_pod=Antalya&var_pod=Antwerpen
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$realstring);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$response = curl_exec($ch);
curl_close($ch);
}
else
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$response = curl_exec($ch);
curl_close($ch);
$fields_str = "";
}
echo $response;
}
but the $realstring variable is empty when i send it by cURL POSTFIELDS
I have no idea wy!

Related

Curl not json response in php

not getting response using curl. I have put all solution but did not get response.
$ch = curl_init();
$header = array('api_key:xxxxxxxxxxxxxxxx','Content-Type: application/json');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
if($postdata!=""){
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
}
$response = curl_exec($ch);
curl_close($ch);
$result = json_decode($response,true);
print_r($result); // not display result
this example not displaying any result but it send to specific place.
Add this in your code and then check.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
For your used-case you have to change your code.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://api.nhs.uk/organisations/FNM60");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$result = json_decode($response,true);
print_r($result);
here we are getting response without echo on the top of the page but we don't need to that type of out put so we have disabled using span tag.
echo "<span style='display:none;'>"; //to hide the curl response
$ch = curl_init();
$header = array('api_key:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx','Content-Type: application/json');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
if($postdata!=""){
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
}
$response = curl_exec($ch);
$info = curl_getinfo($ch);
$responseBody = json_decode($response);
print_r($responseBody);
echo $response; // display 1(one)
curl_close($ch);
echo "</span>";
and result is : 1
check full code

Trying to keep session with curl login in twitter

I'm trying to keep the session along different requests with curl when login into twitter because at some point it logs out from Twitter
$sTarget = "https://twitter.com";
curl_setopt($ch, CURLOPT_URL, $sTarget);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_COOKIEFILE, "/tmp/cookie". $usuario ."_tweet.txt");
curl_setopt($ch, CURLOPT_COOKIEJAR, $usuario ."_tweet.txt");
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_REFERER, $sTarget);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
$html = curl_exec($ch);
if(curl_errno($ch))
{
echo 'error:' . curl_error($c);
}
preg_match('<input name="authenticity_token" type="hidden" value="([a-zA-Z0-9]*)">', $html, $match);
$authenticity_token = $match[1];
if ($authenticity_token == "");
{
preg_match('<input type="hidden" value="([a-zA-Z0-9]*)" name="authenticity_token">', $html, $matchprima);
$authenticity_token = $matchprima[1];
}
$username = $usuario;
$password = "*******";
$sPost = "session[username_or_email]=$username&session[password]=$password&return_to_ssl=true&scribe_log=&redirect_after_login=%2F&authenticity_token=$authenticity_token";
$sTarget = "https://twitter.com/sessions";
curl_setopt($ch, CURLOPT_URL, $sTarget);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $sPost);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: application/x-www-form-urlencoded"));
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
# display server response
$htmldos = curl_exec($ch);
preg_match_all('/^Set-Cookie:\s*([^;]*)/mi', $htmldos, $matches);
$cookies = array();
foreach($matches[1] as $item) {
parse_str($item, $cookie);
$cookies = array_merge($cookies, $cookie);
}
var_dump($cookies);
if(curl_errno($ch))
{
echo 'error:' . curl_error($ch);
}
But when doing a var_dump in cookies to see if the cookies values are the same, under ["_twitter_sess"] I get different strings in the 2 requests. Shouldn't I get the same string if the session is kept between requests? If so, how do I do that?
This code works for logging in Twitter, and keeping session as for doing other things such after it as sending tweets. I've been using it for 6 months without problem. You need simple_html_dom.php from PHP Simple HTML DOM Parser for it to work
<?php
include_once('simple_html_dom.php');
global $usuario;
$usuario = "universohumor2"; //your twitter account username
$ch = curl_init();
/************************************** COMIENZAN LAS LLAMADAS A FUNCIONES *************************************/
/***************************************************************************************************************/
$vector_retorno = logueo_twitter($ch);
/******************************************************************************************************************/
function logueo_twitter($ch)
{
//$ch = curl_init();
global $usuario;
$sTarget = "https://twitter.com";
curl_setopt($ch, CURLOPT_URL, $sTarget);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_COOKIEFILE, "/tmp/cookie". $usuario ."_tweet.txt");
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_REFERER, $sTarget);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
$html = curl_exec($ch);
if(curl_errno($ch))
{
echo 'error:' . curl_error($c);
}
preg_match('<input name="authenticity_token" type="hidden" value="([a-zA-Z0-9]*)">', $html, $match);
$authenticity_token = $match[1];
if ($authenticity_token == "");
{
preg_match('<input type="hidden" value="([a-zA-Z0-9]*)" name="authenticity_token">', $html, $matchprima);
$authenticity_token = $matchprima[1];
}
$username = $usuario;
$password = "password"; // your twitter account password
$sPost = "session[username_or_email]=$username&session[password]=$password&return_to_ssl=true&scribe_log=&redirect_after_login=%2F&authenticity_token=$authenticity_token";
$sTarget = "https://twitter.com/sessions";
curl_setopt($ch, CURLOPT_URL, $sTarget);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $sPost);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: application/x-www-form-urlencoded"));
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
# display server response
$htmldos = curl_exec($ch);
//echo $htmldos;
if(curl_errno($ch))
{
echo 'error:' . curl_error($ch);
}
$vector["sPost"] = $sPost;
$vector["authenticity_token"] = $authenticity_token;
return $vector;
}
?>

Don't show logs with PHP command and cURL

I developed a script with PHP which I use with the console, with the command php index.php.
In this script, I use cURL to query a server.
The problem is my script shows logs on the console, and I just want the result of the echo.
Do you have an idea to hide these logs?
// GET TOKEN
// /////////////////////////////////////////////////////////////////////////////
$url = "some/url";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "$user:$pwd");
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$data = curl_exec($ch);
$location = "";
preg_match_all('/^Location:(.*)$/mi', $data, $location);
$location = trim($location[1][0]);
$location = parse_url($location);
parse_str($location['query'], $attrs);
$token = $attrs['code'];
if( isset($token) ) {
// GET CONNEXION
// /////////////////////////////////////////////////////////////////////////
$url = "some/url";
$post_data = ['code' => $token, 'grant_type' => 'authorization_code'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "$client_id:$client_secret");
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = json_decode(curl_exec($ch));
$connexion = isset($data->access_token) ? $data->access_token : $data->error_description;
// GET LOGIN
// /////////////////////////////////////////////////////////////////////////
$url = "some/url";
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
$data = json_decode($data);
// $login = json_encode(['public' => $data->api_keys[1]->public, 'secret' => $data->api_keys->secret]);
// REQUEST
// /////////////////////////////////////////////////////////////////////////
$url = $request . "?externalId=".$external_id."&externalSource=".$external_source;
date_default_timezone_set("Europe/Paris");
$nonce = generateRandomString();
file_put_contents('php://stderr', print_r("Set random nonce to " . $nonce . "\n", TRUE));
$created = date("Y-m-dTH:i:sP");
$created = date("Y-m-dTH:i:sP");
$username = $data->api->public;
$secret = $data->api->secret;
$pwd_digest = base64_encode(sha1($nonce.$created.$secret));
$auth_header = "X-WSSE: UsernameToken Username=\"$username\", PasswordDigest=\"$pwd_digest\", Nonce=\"$nonce\", Created=\"$created\"";
$header = array($auth_header, 'Accept: something', 'Accept-Language: en');
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$data = curl_exec($ch);
$data = json_decode($data);
echo $data->nbNqPoints;
} else {
echo "0";
}
Answer found at this address: managing curl output in php
I add this line for every cURL instance :
curl_setopt($ch, CURLOPT_VERBOSE, 0);

curl in PHP behave diferrently in the same DOM pages

I run below code and it worked, but I see blank screen when I try with this URL : https://www.reddit.com/r/funny/comments/3aguyp/i_told_my_boyfriend_the_new_puppy_wasnt_allowed/
<?php include('simple_html_dom.php');
$html = getSslPage('https://www.reddit.com/r/pics/comments/3a53qw/grandmother_mother_and_daughter/');
function getSslPage($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
$html = str_get_html($html);
echo $html;
The 2 page have the same DOM structure.

How to use CURL instead of file_get_contents?

I use file_get_contents function to get and show external links on my specific page.
In my local file everything is okay, but my server doesn't support the file_get_contents function, so I tried to use cURL with the below code:
function file_get_contents_curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
echo file_get_contents_curl('http://google.com');
But it returns a blank page. What is wrong?
try this:
function file_get_contents_curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
This should work
function curl_load($url){
curl_setopt($ch=curl_init(), CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
$url = "http://www.google.com";
echo curl_load($url);
I encountered such a problem accessing Google Drive content via the direct link.
After calling file_get_contents returned 302 Moved temporarily
//Any google url. This example is fake for Google Drive direct link.
$url = "https://drive.google.com/uc?id=0BxQKKJYjuNElbFBNUlBndmVHHAj";
$html = file_get_contents($url);
echo $html; //print none because error 302.
With the code below it worked again:
//Any google url. This example is fake for Google Drive direct link.
$url = "https://drive.google.com/uc?id=0BxQKKJYjuNElbFBNUlBndmVHHAj";
$ch = curl_init();
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 3);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$html = curl_exec($ch);
curl_close($ch);
echo $html;
I tested it today, 03/19/2018
//You can try this . It should work fine.
function curl_tt($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 3);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
echo curl_tt("https://google.com");

Categories