How to send information to a REST server via url PHP - php

I have been given a URL that I need PHP to post data to, anonymously, without the end user knowing about it.
The exact structure is:
https://example.com/api/rest/example/createSubscription?email=1#1.com&subscriberNumber=12345JD&subscriberGroup=shop&firstName=Joe&lastName=Bloggs&offerCode=ex1&licenseParameters="STARTDATE%3D2014-08-11%26ENDDATE%3D2014-09-11"
Obviously this is a dynamic URL and I have set it up to be. I am not sure about the best way to approach this issue. Would it be a PUT http_request? I have tried that using the following but it returns a 400 error.
$url = 'https://example.com/api/rest/example/createSubscription?email=1#1.com&subscriberNumber=12345JD&subscriberGroup=shop&firstName=Joe&lastName=Bloggs&offerCode=ex1&licenseParameters="STARTDATE%3D2014-08-11%26ENDDATE%3D2014-09-11"';
$options = array(
'method' => 'PUT',
'timeout' => 15,
'header' => "Content-type: html/txt",
);
$response = http_request($url, $options);

As for your last comment, if the subscription is created simply opening the url in the browser then it is a GET request.
You can perform a GET request using file_get_contents

It's really strange you use PUT method with GET paramater.
After checking php manual here you don't use correctly this methode. that's why the server can't understand your request.
you can look after this function to do a PUT request

Related

Soundcloud resolve issue

I am developing an app for SoundCloud, written in PHP and I use the php-soundcloud library.
After successfully having instantiated a Services_Soundcloud instance, I can do calls like the following:
echo $soundcloud->get('me');
echo $soudcloud->get('users/12345678');
However, the following call is not working:
echo $soundcloud->get('resolve', array('url' => 'https://soundcloud.com/webfordreams'));
The error I get is:
Services_Soundcloud_Invalid_Http_Response_Code_Exception: The requested URL responded with HTTP code 302. in Services_Soundcloud->_request() (line 933 of ../php-soundcloud/Services/Soundcloud.php).
After several hours of debugging I decided to ask for help, as I really don't understand what I do wrong.
Can anybody help me and tell me how to get the proper response?
Leading on from what Francis.G was saying The syntax you are looking for is:
$soundcloud->get('resolve', array('url' => 'https://soundcloud.com/webfordreams'), array(CURLOPT_FOLLOWLOCATION => true);
The only change is in the definition of the curl-ops array passed into the get() function as the third parameter.
The "HTTP/1.1 302" response that you're getting is the appropriate response according to the api.
If you inspect the full php exception you'll get this in the response body:
[httpBody:protected] => {"status":"302 -Found","location":"https://api.soundcloud.com/users/25071103"}
In order to allow CURL to follow the redirect you need to pass the CURLOPT_FOLLOWLOCATION option while calling soundcloud's get() function.
$result = $scloud->get('resolve', array('url' => 'https://soundcloud.com/webfordreams'), array(CURLOPT_FOLLOWLOCATION => TRUE ));
I'm not too sure about the syntax though but what you eventually wanna end up with is something like this in Soundcloud's _request() function:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
Hopefully someone with more experience on this will come along and give you the proper syntax instead of you having to tinker around with the SoundCloud SDK itself.

How to send URL variables programmatically with response from my PHP endpoint

I have a basic API endpoint set up on my site, which a 3rd party site will use to verify certain info that is entered into a form by the user.
Here's the flow:
1. User is on 3rd party site.
2. User enters info into a form
3. Info is sent to my site's endpoint.
4. My site checks the information and returns a JSON object.
As you can see from #4, my API is currently set up to return a JSON object. After the info is checked, something like this happens:
header('content-type: application/json; charset=utf-8');
echo json_encode($response);
exit;
However, the 3rd party site is only set up to receive URL variables. Is there a way to pass back url variables programmatically? I realize I could theoretically send a new request, but it's not clear to me where that request should go (the internal workings of the 3rd party site aren't well documented), so I'd much prefer to send it as a response.
I hope this makes sense. Please comment if it doesn't. Thanks in advance!
You don't get to send GET/POST parameters in the response, but in the response body you can send whatever you want in whatever format you want - and they can use curl or file_get_content and parse it on their side (3rd party's website).
For example (on the 3rd party's website):
//setting a call to your server
$opts = array('http' =>
array(
'method' => 'POST',
'header' => "Content-Type: text/xml\r\n".
"Authorization: Basic ".base64_encode("$https_user:$https_password")."\r\n",
'content' => $body,
'timeout' => 60
)
);
$context = stream_context_create($opts);
$url = 'https://'.$https_server;
// Here they call your server
$result = file_get_contents($url, false, $context, -1, 40000);
// Here you'll parse the $result

cUrl alternatives to get POST answer on a webpage

I would like to get the resulting web page of a specific form submit. This form is using POST so my current goal is to be able to send POST data to an url, and to get the HTML content of the result in a variable.
My problem is that i cannot use cUrl (not enabled), that's why i ask for your knowledge to know if an other solution is possible.
Thanks in advance
See this, using fsockopen:
http://www.jonasjohn.de/snippets/php/post-request.htm
Fsockopen is in php standard library, so all php fron version 4 has it :)
try file_get_contents() and stream
$opts = array( 'http'=>array('method'=>"POST", 'content' => http_build_query(array('status' => $message)),));
$context = stream_context_create($opts);
// Open the file using the HTTP headers set above
$file = file_get_contents('http://www.example.com/', false, $context);

how to set user agent for get_headers PHP function

I know It's easy to set user agent for curl but my code is based on get_headers, by default get_headers user agent is empty.
thanks for any help.
Maybe this?
ini_set('user_agent', 'Mozilla/5.0');
For anyone else coming here, the best option (instead of a server-wide change, which who knows what might break), is to use stream context options (the user agent option, in particular).
The PHP documentation already shows an example for change the HTTP method (sadly, also using a global setting 🤦).
In any case, the code would be something like:
$context = stream_context_create([
'http' => [
'user_agent' => 'Mozilla/5.0'
]
]);
$headers = get_headers('http://example.com', true, $context);
get_headers only specifies the data sent by the server to the client (in this case, PHP), it doesn't specify request headers.
If you're trying to find the user agent the get_headers request was made with, you'll have to use:
ini_get('user_agent');
For more documentation see the links below:
http://us3.php.net/get_headers
http://us3.php.net/manual/en/filesystem.configuration.php#ini.user-agent

How to send a PUT request with a file and an array of data with PHP

I've set up a REST service and client in PHP and I'm having a bit of trouble with PUT.
Here's my situation:
I'm coding a REST resource that should accept an array of data and an image. The REST resource should update an existing record, so I'm using PUT. I'm sending the data with a PHP curl client I wrote. So - pretty much the same situation as if you were sending a HTML multipart form to a PHP script that does a file upload and accepts some additional POST fields - except with PUT and PHP curl..
Up 'till now I've been sending the PUT request something like this (pseudo code):
$a_some_data = array('name' => 'test', 'user_id' => 4);
$body = http_build_query($a_data);
$fh = fopen('php://memory', 'rw');
fwrite($body);
rewind($fh);
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => 'http://myapi/resource/someid',
CURLOPT_PUT => TRUE,
CURLOPT_INFILE => $fh,
CURLOPT_INFILESIZE => strlen($body)
));
curl_exec($ch);
and reading the data on the server like so:
parse_str(file_get_contents('php://input'), $put_data);
..which works just fine.
So now I would like to add a (binary) file into the mix.
- How would I implement this on the client side?
- How would I deal with the file on the server?
For a test I set up a HTML form with a file input, copied the raw multipart/form-data request it sends, and tried sending that data as a file with curl in a PUT request. That kind of works, but I would have to parse the raw data on the server manually, which I'm not sure is the best idea. Alternatively, I guess I could send the file as the body of the PUT request, and add the other parameters in the URL as a query string - but I guess that kind of defies the point of a PUT REST resource..
Please share your thoughts on this.
Thanks!
There are at least two other ways unless your original version isn't enough (since libcurl should deal just fine with binary files too with that script). Note that how you decide receive the PUT in the receiving end is not a curl issue so I'll leave it out of this response.
1 - Like you started out, but provide a CURLOPT_READFUNCTION with which you feed the data to libcurl that it will send.
2 - Use CURLOPT_POSTFIELDS (with a string) and make it look like a POST, and then you change the HTTP method with CURLOPT_CUSTOMREQUEST to "PUT"

Categories