How do I get the content portion of my http header? - php

I'm trying to get the content header query string returned. I'm getting a response but it does not include the query string I am trying to pass. I'm running on an Apache server.
$postdata = http_build_query(
array(
'feed_them_social' => 'yes',
'refresh_token' => get_option( 'custom_refresh_token' ),
'time' => esc_html( get_option( 'custom_token_exp_time' ) ),
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$command = 'curl --data "' . $context . '" https://my-url.com';
exec($command, $token);
$output = implode('', $token);
print_r( $output );
I added this to php file on the server I am trying to get a response from. I just want to print out the header content with my query string for now but not having any luck.
$headers = apache_request_headers();
foreach ($headers as $header => $value) {
echo "$header: $value <br />\n";
}

stream_context_create doesn't generate a string which you can concatenate onto a command with the . operator. What it does is sets up a "resource" (a memory structure, a bit like an object where everything is private) for use with selected PHP functions, such as file_get_contents.
If you want to use curl, use PHP's built-in curl functions, or a more user-friendly library like Guzzle.
If for some reason you really need to construct a set of command-line options, you have to do that yourself (and deal with all the risks of accidentally running the wrong thing), PHP doesn't know how to generate the right argument for curl or any other command.

Related

How do I get the content header from my POST?

I'm trying to get the content header query string returned. I'm getting a response but it does not include the query string I am trying to pass. I'm running on an Apache server.
$postdata = http_build_query(
array(
'feed_them_social' => 'yes',
'refresh_token' => get_option( 'custom_refresh_token' ),
'time' => esc_html( get_option( 'custom_token_exp_time' ) ),
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$command = 'curl --data "' . $context . '" https://my-url.com';
exec($command, $token);
$output = implode('', $token);
print_r( $output );
Below is the response I'm getting.
Content-Length: 15
Content-Type: application/x-www-form-urlencoded
Accept: */*
Host: youtube-token-refresh.myurl.com
User-Agent: curl/7.77.0
I added this to php file on the server I am trying to get a response from. I just want to print out the header content with my query string for now but not having any luck. What am I missing?
$headers = apache_request_headers();
foreach ($headers as $header => $value) {
echo "$header: $value <br />\n";
}
Using exec is not secure and also it's a hard way to use curl.
In this case, I suggest file_get_contents function that works fine with stream_context_create. More Information: https://www.php.net/manual/en/function.file-get-contents.php
Just replace the last 4 lines with the following code.
$output = file_get_contents('https://my-url.com', false, $context);
print_r($output);

How to issue Shopify API REST post using file_get_contents()?

I am building a web app that uses Shopify's API, and while I can easily issue GET requests to display product information using the file_get_contents() function in PHP, I'm not having any luck using it to issue POST requests to update the inventory quantity of a product from the webapp. I would prefer to use this method if it is possible instead of installing an additional library/extension to use cURL. Here's my code:
$postdata = http_build_query(
array(
'location_id' => $locationID,
'inventory_item_id' => $itemID,
'inventory_adjustment' => 99
)
);
$opts = array('https' =>
array(
'method' => 'POST',
'header' => "Content-Type: application/json\r\n" .
"Accept: application/json\r\n" .
"Content-Length: " . strlen($postdata) . "rn",
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = file_get_contents('https://' . API_key() . ':' . API_password() . '#' . $store . '.myshopify.com/admin/api/2021-04/inventory_levels/adjust.json', false, $context);
json_decode($result, true);
if ($result) {
echo $result;
} else {
echo "post failed<br>";
}
And because I can get the product info via a GET request I know the API key, password, and URL structure are okay. Any help would be greatly appreciated (I suspect the error lies in the header section somewhere). The above code returns false each time.

PHP Return XML with stream_context_create()

I'm using stream_context_create() instead of cURL to make an HTTP post. It works, but file_get_contents() returns a single string of all the data instead of an XML object. How can I modify my code so it returns an XML object and I can access the values individually?
<?php
$url = 'http://www.domain.com';
$data = array( 'name1' => 'data1', 'name2' => 'data2', 'name3' => 'data3' );
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query( $data )
)
);
$context = stream_context_create( $options );
$result = file_get_contents( $url, false, $context );
var_dump($result); // data1data2data3
?>
And the page that returns the XML:
<?php
header('Content-type: text/xml');
$xml = '<result><name1>' . $_POST['data1'] . '</name1><name2>' . $_POST['data2'] . '</data2><name3>' . $_POST['data3'] . '</name3></result>';
echo $xml;
?>
I think that you're looking at this var_dump in your browser, and browser tries to render tags <result> and <name..> as standard HTML.
Please try to run this code, and check the source of your webpage (in Chrome, you can do it by prepending page URL by view-source:)

PHP POST API call

Trying to make a POST API call and I get the error file_get_contents(http://api.turfgame.com/v4/users): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request. API info can be found at http://api.turfgame.com
<?php
$data = array("name" => "DIProgan");
$url = 'http://api.turfgame.com/v4/users';
$options = array(
'http' => array(
'method' => 'POST',
'content' => json_encode( $data ),
'header'=> "Content-Type: application/json\r\n" .
"Accept: application/json\r\n"
)
);
$context = stream_context_create( $options );
$result = file_get_contents( $url, false, $context );
$response = json_decode( $result );
var_dump($response);
?>
Using XAMPP & PHP 5.4.4. I tried using "requests for PHP" since I liked the Python version of it but it just complains about not finding hook functions. People seem to hate cURL and from the little I need it seemed easier without it. Maybe not. Help?
It seems like you have just created an array where you have put your parameter. It should be stored within an object first.

PHP HTTP Request (stream_context_create)

Hi I am trying to get the following to work. It is making the HTTP request because I can see it at the other end but it is not passing over the two values and parameters.
Can anyone help?
<?php
$url = "http://example.com/ws.php?uid=0000&pin=0000";
$fields = array(
'target1' => $_GET['target1'],
'target2' => $_GET['target2']);
$data = http_build_query($fields);
$context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $data,
)
));
$result = file_get_contents($url, false, $context);
echo $data."<br />";
echo($result);
?>
If PHP 5.2, see PHP bug https://bugs.php.net/bug.php?id=41051
Description:
the http wrapper ignores 'header' option in stream_create_context( )
if the value is not given as a simple array; neither a string nor a
map (e.g. array('X-My-Header' => 'test')) will cause the additional
headers to be sent.
Based on the examples i've seen you need a \r\n at the end of the header value.
'header' => "Content-type: application/x-www-form-urlencoded\r\n"
apart from that you will also need to provide
"Content-Length: " . strlen($data) . "\r\n",

Categories