JSON not showing image from this - php

my code wont show the screenshot header('Content-Type:image/png');
$url = 'https://www.google.com/';
//API Url
$url2 = 'http://api.site:port/screenshot';
//Initiate cURL.
$ch = curl_init($url2);
//The JSON data.
$jsonData = array(
'url' => $url
);
//Encode the array into JSON.
$jsonDataEncoded = json_encode($jsonData);
//Tell cURL that we want to send a POST request.
curl_setopt($ch, CURLOPT_POST, 1);
//Attach our encoded JSON string to the POST fields.
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
//Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('content-type: application/json'));
//Execute the request
$result = curl_exec($ch);
curl_close($ch);
i have tested on postman this shows correct just not on php/json any ideas?

Related

Openfigi php request -> Request body must be a JSON array

I am trying to use the openfigi api with php. I keep getting this error message: "Request body must be a JSON array.". Any ideas on how to solve this? I have tried several solutions.
$curlUrl = 'https://api.openfigi.com/v2/mapping';
$data = array('idType' => 'ID_WERTPAPIER', 'idValue' => '851399', 'exchCode' => 'US');
$j = json_encode($data);
//$apiToken = 'X-OPENFIGI-APIKEY: xxx';
$httpHeadersArray = Array();
$httpHeadersArray[] = 'Content-Type: application/json';
//$httpHeadersArray[] = $apiToken;
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $curlUrl);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $j);
curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeadersArray);
$res = curl_exec($ch);
echo "<pre>";
print_r($res);
echo "</pre>";
had the same problem, but solved it.
This is my working code:
// The url you wish to send the POST request to
$url = 'https://api.openfigi.com/v2/mapping/';
// Create a new cURL resource
$ch = curl_init($url);
// The data to send to the API
$postData = array(
array(
"idType" => "ID_ISIN",
"idValue" => "US4592001014"
)
);
// Setup cURL
curl_setopt_array($ch, array(
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_POSTFIELDS => json_encode($postData)
));
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
print_r(json_encode($postData));
// Send the request
$response = curl_exec($ch);
// Check for errors
if($response === FALSE){
die(curl_error($ch));
}
// Decode the response
$responseData = json_decode($response, TRUE);
// Print the date from the response
print_r($responseData);

Unable to Upload a file to sharepoint library (Office 365) , gives error while creeating form digest

```` echo "<br/><br/>Generating Form Digest<br/>";
// Initialize curl for Getting the Form Digest
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, "https://xxxxxxxxxxxxxx.sharepoint.com/_api/contextinfo");
// Set curl Method
curl_setopt($ch, CURLOPT_POST, true);
// Set HTTP Header for POST request
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Length: 0',
'Accept : application/json;odata=verbose',
// 'Authorization: Bearer ' . $access_token,
));
curl_setopt($ch, CURLOPT_POSTFIELDS, $url_client);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
echo "<br/> Result of API Context :<br/>";
print_r(json_decode($result));
curl_close($ch);
echo "<br/><br/><br/>Uploading File<br/>";
// API URL with the file attached
$api_URL =
"https://xxxxxxxxxxxxxxxxxxxxxxxxxx.sharepoint.com/_api/web/GetFolderByServerRelativeUrl
('Documents')/Files/add(url='uploads/{$_FILES["fileToUpload"]["name"]}',overwrite=true)";
echo $api_URL;
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $api_URL );
curl_setopt($ch, CURLOPT_POST, true);
// Set HTTP Header for POST request
// Set the headers in the curl object
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
// 'Content-Type: application/x-www-form-urlencoded',
"Authorization: Bearer {$json_result->access_token}",
"Content-Length: {$filesize}",
"X-HTTP-Method: MERGE",
//"X-RequestDigest:",
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//execute post
$result = curl_exec($ch);
echo "<br/> RESULT OF Upload file CURL<br/> " ;
// Convert result to JSon
$json_result = json_decode($result);
// Print Json Result
print_r($json_result);
// Close CURL
curl_close($ch);````
What I'm trying to do is to create a PHP webpage that will take a file ans an iput and upload it directly yo a shrepoint library
I'm generating access token before this code is executed
I'm getting the "404 unauthorized" error when form digest is generated, please help !!

How to Pass json Dtat in query param in php for pull service

I'm trying to get the data from pull service in PHP while i"m calling the end point in postman I'm getting the output
but while I'm trying to call using PHP I'm getting the 500
i have attaching the PHP code please guide me where I'm doing wrong
//API Url
$url = 'https://abc.xyz/vlet?Info=';
//The JSON data.
$jsonData =
array(
'NAME' => 'anish',
'Key' => 'vQOgCmDgxxdw',
'authToken' => 'xswSgsG8Dtz05Rfhfzr83t25',
'work_date' => '2020-07-07'
);
//print_r( $jsonData); exit;
//Encode the array into JSON.
$jsonDataEncoded = json_encode($jsonData);
$full_url = $url.$jsonDataEncoded ;
//echo $full_url ;
$result1 = file_get_contents($full_url);
print_r( $result1);
//Initiate cURL.
$curl = curl_init();
$ch = curl_setopt($curl, CURLOPT_URL, $full_url);
//$ch = curl_init($url);
//Tell cURL that we want to send a POST request.
curl_setopt($ch, CURLOPT_POST, 1);
//Attach our encoded JSON string to the POST fields.
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
//Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
//Execute the request
$result = curl_exec($ch);
print_r($result);

Curl request works in Rested chrome extension but not in php

everyone, i am requesting data from some other website but I am unable to request through curl it shows error 500 but when I do the same request through chrome RESTED extension it shows me output
$url = "XXXXXXXXXXX";
// what post fields?
$fields = array(
'roll_number' => '123456',
'full_name' => 'aaaaaa',
'mother_name' => 'bbbbbbb',
'_token' => 'xxxxxxxxxxxxxxxx'
);
// build the urlencoded data
$postvars = http_build_query($fields);
// open connection
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$postvars);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
echo $response;
Request should be by post method and data should be JSON encoded or URL encoded I have tried both but none of them worked
Not sure if this is the cause, but shouldn't your HTTPHEADER be application/json? Also, you're not encoding the data as JSON - you might need to change $postvars = http_build_query($fields); to $postvars = json_encode($fields);
I'd also take a look at https://lornajane.net/posts/2011/posting-json-data-with-php-curl

POST JSON data via CURL and grabbing it

I am trying to pass a json data as param for cURL POST. However, I am stuck at grabbing it and saving it on db.
cURL file:
$data = array("name" => "Hagrid", "age" => "36");
$data_string = json_encode($data);
$url = 'http://localhost/project/test_curl';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json')
);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
$result = curl_exec($ch);
//based on http://www.lornajane.net/posts/2011/posting-json-data-with-php-curl
test_curl file:
$order_info = $_POST; // this seems to not returning anything
//SAVE TO DB... saving empty...
What did I miss? Weew....
You are sending the data as raw JSON in the body, it will not populate the $_POST variable.
You need to do one of two things:
You can change the content type to one that will populate the $_POST array
You can read the raw body data.
I would recommend option two if you have control over both ends of the communication, as it will keep the request body size to a minimum and save bandwidth over time. (Edit: I didn't really emphasize here that the amount of bandwidth it will save is negligible, only a few bytes per request, this would only be a valid concern is very high traffic environments. However I still recommend option two because it is the cleanest way)
In your test_curl file, do this:
$fp = fopen('php://input', 'r');
$rawData = stream_get_contents($fp);
$postedJson = json_decode($rawData);
var_dump($postedJson);
If you want to populate the $_POST variable, you will need to change the way you send the data to the server:
$data = array (
'name' => 'Hagrid',
'age' => '36'
);
$bodyData = array (
'json' => json_encode($data)
);
$bodyStr = http_build_query($bodyData);
$url = 'http://localhost/project/test_curl';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/x-www-form-urlencoded',
'Content-Length: '.strlen($bodyStr)
));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $bodyStr);
$result = curl_exec($ch);
The raw, undecoded JSON will now be available in $_POST['json'].
Use following php function for posting data using php curl function in x-www-form-urlencoded format.
<?php
$bodyData = http_build_query($data); //for x-www-form-urlencoded
?>

Categories