PHP Curl_exec output is getting trimmed - php

I am hitting my content server which sends multipart data.
When server response is small ( < 40KB ) , I am getting full response data in $response
string. But when server response data is large ( > 112KB) its getting trimmed to 48KB
and my whole multipart data parsing is failing.
What should I do to get the whole 112KB data in $response string ?
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "spider", // who am i
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
);
$ch = curl_init( $this->url );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $arr_param);
$response = curl_exec( $ch );
$err = curl_errno( $ch );
$errmsg = curl_error( $ch );
$header = curl_getinfo( $ch );
parseData($response);
Please help, what i am doing wrong here.

Related

Fetch JSON response from API using php

I'm trying to fetch data from API, but no data returns.
I try the API request in postman and it's working but in my php code nothing gets returned.
$uri = 'https://example.com';
$opts = array(
'http' => array(
'method'=>'POST',
'header'=>'Content-Type: application/x-www-form-urlencoded',
)
);
$context = stream_context_create($opts);
$result = file_get_contents($uri, false, $context);
print $result;
or using curl
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://example.com',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
the error is
file_get_contents(https://example.com): failed to open stream: HTTP request failed! HTTP/1.1 411 Length Required
Note: the response size in postman is 9.83, is this maybe an issue?
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://www.example.com/tester.phtml");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
"postvar1=value1&postvar2=value2&postvar3=value3");
// In real life you should use something like:
// curl_setopt($ch, CURLOPT_POSTFIELDS,
// http_build_query(array('postvar1' => 'value1')));
// Receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
curl_close ($ch);
// Further processing ...
if ($server_output == "OK") { ... } else { ... }
The issue has been solved by add Content-Length: 0 in the header

PHP print_r not returning json request

The following code runs without any errors, however it doesn't return anything.
If I paste it into my browser it returns the information I'm looking for, I can also replace it with another URL and it works perfectly.
$ch = curl_init();
$url = 'https://api.coronavirus.data.gov.uk/v1/data?filters=areaName=Somerset&structure={"date":"date","areaName":"areaName","areaCode":"areaCode","newCasesByPublishDate":"newCasesByPublishDate","cumCasesByPublishDate":"cumCasesByPublishDate","newDeathsByDeathDate":"newDeathsByDeathDate","cumDeathsByDeathDate":"cumDeathsByDeathDate"}';
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$resp = curl_exec($ch);
$decode =json_decode($resp);
print_r($decode);
curl_close($ch);
You can use following generated code by the postman
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://api.coronavirus.data.gov.uk/v1/',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_POSTFIELDS =>'{"date":"date","areaName":"areaName","areaCode":"areaCode","newCasesByPublishDate":"newCasesByPublishDate","cumCasesByPublishDate":"cumCasesByPublishDate","newDeathsByDeathDate":"newDeathsByDeathDate","cumDeathsByDeathDate":"cumDeathsByDeathDate"}',
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
This code is worked. Reason is encoding of received content.
try {
$ch = curl_init();
// Check if initialization had gone wrong*
if ( $ch === false ) {
throw new RuntimeException( 'failed to initialize' );
}
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL,
'https://api.coronavirus.data.gov.uk/v1/data?filters=areaName=Somerset&structure={"date":"date","areaName":"areaName","areaCode":"areaCode","newCasesByPublishDate":"newCasesByPublishDate","cumCasesByPublishDate":"cumCasesByPublishDate","newDeathsByDeathDate":"newDeathsByDeathDate","cumDeathsByDeathDate":"cumDeathsByDeathDate"}' );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Solution is here. Response is the compressed content which your curl failed to detect. Empty encoding means to handle any type of encoding. It should solve your issue.
curl_setopt($ch, CURLOPT_ENCODING, '');
$content = curl_exec( $ch );
curl_close( $ch );
if ( $content === false ) {
throw new RuntimeException( curl_error( $ch ), curl_errno( $ch ) );
}
/* Process $content here */
$decode = json_decode( $content );
var_dump( $decode );
// Close curl handle
curl_close( $ch );
} catch ( RuntimeException $e ) {
trigger_error(
sprintf(
'Curl failed with error #%d: %s',
$e->getCode(),
$e->getMessage()
),
E_USER_ERROR
);
}

cURL POST method not working

I am using cURL to call a REST API. And the below code works for GET method , but however for POST method call it doesn't work after i add :
curl_setopt($ch, CURLOPT_POST, true);
What could be the cause ?
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "spider", // who am i
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
CURLOPT_SSL_VERIFYPEER => false, // Disabled SSL Cert checks
CURLOPT_HTTPHEADER => array('appId: C579929D-F0AA-4A8F-B6C6-1FF96694483C','appKey: AE78E7F1-FBDE-45F5-BAC2-210CEE9D3ED9')
);
$url = "some-url";
$ch = curl_init($url);
curl_setopt_array($ch, $options);
curl_setopt($ch, CURLOPT_POST, true);
$output = curl_exec($ch);
echo $output;
P.S
I double checked the API call using PostMan and it returns the expected result.
Do i need to add additional properties to API call ?
And i have to call REST API of format :
https://api.abc.com/v1/transactions/companyid/123/userid/456/description/ppeck/‌​source/PPA
And as suggested in the answer i have updated my code as :
$url = "https://api.abc.com/v1/transactions";
$ch = curl_init($url);
curl_setopt_array($ch, $options);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, "companyid=12301&userid=31401&description=ppa-check&source=PPA&amount=123&txndate=010111&txntypeid=420&customerid=2701");
$output = curl_exec($ch);
And sadly it didn't worked.
try
post fields must be as a query string with appending (&) like
$url = 'http://example.com'; //your api call url
$fields = 'q=example&y=fgt&ghty'; // all posting fields separated with &
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);

Getting SESSION_ID from skrill server

Please help me i am having some trouble.
What i want to do is integrate moneybooker in my website.
I studied the "Merchant Integration Manual version 6.17" in 2.3.2 Topic where i have to create and get a SESSION_ID form skrill server by sending payment parameters by post and get the SESSION_ID this session will hold my transaction information like amount and my account.
By using below code i can't retrieve the SESSION_ID from their server.
$url = "https:www.moneybookers.com/app/payment.pl";
$post_data = array (
"prepare_only" => 1,
"amount" => 10,
"currency" => 'USD',
"detail1_description" => "Description",
"detail1_text" => "Text",
"pay_to_email" => "****my**accoutn#yahoo.com"
);
$head = get_web_page($url, $post_data);
echo "<pre>";
print_r($head);
echo "</pre>";
function get_web_page( $url, $post_data )
{
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "spider", // who am i
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
);
$ch = curl_init( $url );
curl_setopt_array( $ch, $options );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//we are doing a POST request
curl_setopt($ch, CURLOPT_POST, 1);
//adding the post variables to the request
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$content = curl_exec( $ch );
$err = curl_errno( $ch );
$errmsg = curl_error( $ch );
$header = curl_getinfo( $ch );
curl_close( $ch );
$header['errno'] = $err;
$header['errmsg'] = $errmsg;
$header['content'] = $content;
return $header;
}
The above code works for other sites but not for moneybooker.
But when i submit simple html form, session id is created and shown but i am not being redirected to my site !!
Try adding cookies support
$fh = fopen("cookies.txt", "a+") or die("Can't open file!");
fclose($fh);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookies.txt");
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies.txt");
When you set CURLOPT_POSTFIELDS with an array, the request will be sent as multipart/form-data, try changing that line to:
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data));

How to create the REST API for this curl operation in PHP

I want executed this simple curl operation through php how can i execute this in simple php not any framework , can i do it in simple php or do i need the framework?
curl -XPOST localhost:12060/repository/schema/fieldType -H 'Content-Type: application/json' -d '
{
action: "create",
fieldType: {
name: "n$name",
valueType: { primitive: "STRING" },
scope: "versioned",
namespaces: { "my.demo": "n" }
}
}' -D -
what i tried is :
<?php
$url="localhost:12060/repository/schema/fieldType";
//open connection
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "spider", // who am i
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
);
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
$fieldString=array(
"action"=> "create",
"fieldType"=>array(
"name"=> "n$name",
"valueType"=> array( "primitive"=> "STRING" ),
"scope"=> "versioned",
"namespaces"=> array( "my.demo"=> "n" )
)
);
//for some diff
curl_setopt($ch, CURLOPT_HTTPHEADERS, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode("{json: $fieldString}"));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt_array( $ch, $options );
//execute post
$result = curl_exec($ch);
$header = curl_getinfo( $ch );
echo $result;
//close connection
curl_close($ch);
?>
But it is giving me this
The given resource variant is not supported.Please use one of the following: * Variant[mediaType=application/json, language=null, encoding=null]
Your problem lies here:
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode("{json: $fieldString}"));
The $fieldString is not actually an string as its name implies. It was an array at this point still. And you are trying to reencode an mangled faux json string, with just Array as data. Won't work.
Instead use this to get the desired(?) effect:
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fieldString));

Categories