I am having some trouble working with curl and headers returned by servers.
1) My php file on my_website.com/index.php looks like this (trimmed version):
<?php
$url = 'http://my_content_server.com/index.php';
//Open connection
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
echo $result;
?>
The php file on my_content_server.com/index.php looks like this:
<?php
header("HTTP/1.0 404 Not Found - Archive Empty");
echo "Some content > 600 words to make chrome/IE happy......";
?>
I expect that when I visit my_website.com/index.php, I should get a 404, but that is not happening.
What am I doing wrong?
2) Basically what I want to achieve is:
my_content_server.com/index.php will decide the content type and send appropriate headers, and my_website.com/index.php should just send the same content-type and other headers (along with actual data) to the browser. But it seems that my_website.com/index.php is writing its own headers? (Or maybe I am not understanding the working correctly).
regards,
JP
Insert before curl_exec():
curl_setopt($ch,CURLOPT_HEADER,true);
Instead of just echo'ing the result, forward the headers to the client as well:
list($headers,$content) = explode("\r\n\r\n",$result,2);
foreach (explode("\r\n",$headers) as $hdr)
header($hdr);
echo $content;
Related
I am trying to send from a php code running on a nginx server a get request to another remote django server. I am expecting to get data in REST API using serializer.
First if I open this link in my local browser:
http://192.168.2.0:8000/myapp/documents/
I will get:
HTTP 200 OK
Allow: GET, POST, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept
[
{
"id": "000e3588-9544-4df6-a589-cc0166242b5b",
"docfile": "/media/documents/2017/06/30/DSC03623.JPG"
},
{
"id": "3dc6be9f-8659-41d8-8282-b64662032da6",
"docfile": "/media/documents/2017/06/30/DSC03611_9KWbftQ.JPG"
},
{
"id": "28eacb2d-0798-46e9-b63e-10ff704482ce",
"docfile": "/media/documents/2017/06/30/DSC03555.JPG"
}
]
and this is exactly the info I want to get when i run my php code.
This is what I was working on, the commented parts are previous failed attempts:
<?php
echo "<h1>I am here</h1>";
$url = "http://192.168.2.0:8000/myapp";
/*$body = http_parse_message(http_get($url))->body;
$body = http_get($url);
echo $body;*/
/*$client = new Client($url);
$request = $client->newRequest('/documents/');
$response = $request->getResponse();
echo $response->getParsedResponse();*/
$r = new HttpRequest('http://192.168.2.0:8000/myapp/documents/', HttpRequest::METH_GET);
$r->send();
echo $r->getResponseCode();
if ($r->getResponseCode() == 200)
{
echo "<h2>we get 200 response</h2>";
}
?>
non of the above 3 attempts printed anything except the first echo
I am here
Ok, finally this is the code that worked for me:
<?php
$ch = curl_init('http://192.168.2.0:8000/myapp/documents/');
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
echo $data;
?>
it needs cUrl to work.
cUrl usual enabled in php. To double check
run this code as a script:
<?php
phpinfo();
?>
search in in the results if cUrl is working.
I want to pass a string from one PHP file to another using $_GET method. This string has different value each time it is being passed. As I understand, you pass GET parameters over a URL and you have to explicitly tell what the parameter is. What if you want to return whatever the string value is from providing server to server requesting it? I want to pass in json data format. Additionally how do I send it as Ajax?
Server (get.php):
<?php
$tagID = '123456'; //this is different every time
$tag = array('tagID' => $_GET['tagID']);
echo json_encode($tag);
?>
Server (rec.php):
<?php
$url = "http://192.168.12.169/RFID2/get.php?tagID=".$tagID;
$json = file_get_contents($url);
#var_dump($json);
$data = json_decode($json);
#var_dump($data);
echo $data;
?>
If I understand correctly, you want to get the tagID from the server? You can simply pass a 'request' parameter to the server that tells the server what to return.
EDIT: This really isn't the proper way to implement an API (like, at all), but for the sake of answering your question, this is how:
Server
switch($_GET['request']) {
case 'tagID';
echo json_encode($tag);
break;
}
You can now get the tagID with a URL like 192.168.12.169/get.php?request=tagId
Client (PHP with CURL)
When it comes to the client it gets a bit more complicated. You mention AJAX, but that will only work for JavaScript. Your php file can't use AJAX, you'll have to use cURL.
$request = "?request=tagID";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, '192.168.12.169/get.php' . $request);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, '3');
$content = trim(curl_exec($ch));
curl_close($ch);
echo $content;
EDIT: added the working cURL example just for completeness.
Included cURL example from: How to switch from POST to GET in PHP CURL
Client (Javascript with AJAX)
$.get("192.168.12.169/get.php?request=tagId", function(data) {
alert(data);
});
I want to get the whole element <article> which represents 1 listing but it doesn't work. Can someone help me please?
containing the image + title + it's link + description
<?php
$url = 'http://www.polkmugshot.com/';
$content = file_get_contents($url);
$first_step = explode( '<article>' , $content );
$second_step = explode("</article>" , $first_step[3] );
echo $second_step[0];
?>
You should definitely be using curl for this type of requests.
function curl_download($url){
// is cURL installed?
if (!function_exists('curl_init')){
die('cURL is not installed!');
}
$ch = curl_init();
// URL to download
curl_setopt($ch, CURLOPT_URL, $url);
// User agent
curl_setopt($ch, CURLOPT_USERAGENT, "Set your user agent here...");
// Include header in result? (0 = yes, 1 = no)
curl_setopt($ch, CURLOPT_HEADER, 0);
// Should cURL return or print out the data? (true = retu rn, false = print)
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Timeout in seconds
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
// Download the given URL, and return output
$output = curl_exec($ch);
// Close the cURL resource, and free system resources
curl_close($ch);
return $output;
}
for best results for your question. Combine it with HTML Dom Parser
use it like:
// Find all images
foreach($output->find('img') as $element)
echo $element->src . '<br>';
// Find all links
foreach($output->find('a') as $element)
echo $element->href . '<br>';
Good Luck!
I'm not sure I get you right, But I guess you need a PHP DOM Parser. I suggest this one (This is a great PHP library to parser HTML codes)
Also you can get whole HTML code like this:
$url = 'http://www.polkmugshot.com/';
$html = file_get_html($url);
echo $html;
Probably a better way would be to parse the document and run some xpath queries over it afterwards, like so:
$url = 'http://www.polkmugshot.com/';
$xml = simplexml_load_file($url);
$articles = $xml->xpath("//articles");
foreach ($articles as $article) {
// do sth. useful here
}
Read about SimpleXML here.
extract the articles with DOMDocument. working example:
<?php
$url = 'http://www.polkmugshot.com/';
$content = file_get_contents($url);
$domd=#DOMDocument::loadHTML($content);
foreach($domd->getElementsByTagName("article") as $article){
var_dump($domd->saveHTML($article));
}
and as pointed out by #Guns , you'd better use curl, for several reasons:
1: file_get_contents will fail if allow_url_fopen is not set to true in php.ini
2: until php 5.5.0 (somewhere around there), file_get_contents kept reading from the connection until the connection was actually closed, which for many servers can be many seconds after all content is sent, while curl will only read until it reaches content-length HTTP header, which makes for much faster transfers (luckily this was fixed)
3: curl supports gzip and deflate compressed transfers, which again, makes for much faster transfer (when content is compressible, such as html), while file_get_contents will always transfer plain
I've got the OneNote API PHP Sample (thanks jamescro!) working with all the POST examples, but there's no GET example and I haven't managed to put together code of my own that works. Here's what I've tried without success:
// Use page ID returned by POST
$pageID = '/0-1bf269c43a694dd3aaa7229631469712!93-240BD74C83900C17!600';
$initUrl = URL . $pageID;
$cookieValues = parseQueryString(#$_COOKIE['wl_auth']);
$encodedAccessToken = rawurlencode(#$cookieValues['access_token']);
$ch = curl_init($initUrl);
curl_setopt($ch, CURLOPT_URL, $initUrl); // Set URL to download
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if (! $response === false) {
curl_close($ch);
echo '<i>Response</i>: '. htmlspecialchars($response);
}
else {
$info = curl_getinfo($ch);
curl_close($ch);
echo '<i>Error</i>: ';
echo var_export($info);
}
It just returns 'Error' with an info dump. What am I doing wrong?
without information on the specific error I'm not sure what issue you are hitting. Try looking at the PHP Wordpress plugin here: https://github.com/wp-plugins/onenote-publisher/blob/master/api-proxy.php
look at what is sent to wp_remote_get - there are necessary headers that are needed.
Also make sure you have the scope "office.onenote" when you request the access token.
If you need more help, please add information about the specific URL you are attempting to call, as well as the contents of your headers. If you have any errors, please include the output.
Solved:
As Jay Ongg pointed out, "there are necessary headers that are needed".
After adding more detailed error checking and getting a 401 response code, I added:
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:text/html\r\n".
"Authorization: Bearer ".$encodedAccessToken));
... and could access the requested page.
I'm having a bit of a problem reading JSON data that I generate in PHP and then pass back to my Javascript and I'm not sure why.
Here is the PHP:
header("content-type: text/json");
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
//execute post
$result = curl_exec($ch);
// echo the result as a JSON object
echo json_encode($result);
And here is the Javscript:
$.post("payment_do.php", { "token": response.response.token, "ip_address": response.ip_address}).done(function(data) {
console.log(data);
});
It seems as though if I take the header line away in the PHP I get a response that I can read in the Javascript but I cannot access any of the elements the way you would expect. If I leave the header in there, I get no response readable by the javascript.
EDIT: Now getting a response from the php, looks like this:
"{\"response\":{\"token\":\"ch_9knTXHoU0dVZsl7iMHyHGg\",\"success\":true,\"amount\":9900,\"currency\":\"AUD\",\"description\":\"test\",\"email\":\"test#test.com\",\"ip_address\":\"1.1.1.1\",\"created_at\":\"2013-03-18T23:49:12Z\",\"status_message\":\"Success!\",\"error_message\":null,\"card\":{\"token\":\"test_token\",\"display_number\":\"XXXX-XXXX-XXXX-0000\",\"scheme\":\"master\",\"address_line1\":\"123 Fake Street Fakington\",\"address_line2\":null,\"address_city\":\"moon\",\"address_postcode\":\"2121\",\"address_state\":\"NSW\",\"address_country\":\"Australia\"},\"transfer\":[],\"amount_refunded\":0,\"total_fees\":999,\"merchant_entitlement\":999,\"refund_pending\":false}}"
In the end I had to remove the json_encode function in the php and just return the result. In the javascript (using jQuery) I then called:
data = $.parseJSON(data);
With which I could then access the elements of the object.
Change your header to application/json (as opposed to text/json). In addition, if you actually want to further process the results of the curl_exec call then you need to set an additional option:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
This will cause the curl_exec call to return data on success, as opposed to TRUE.
$result = curl_exec($ch);
if(! $result) {
// handle error
}
echo json_encode($result);
Also, you will want to verify the data you are getting back from the cURL call - make sure that it really needs to be encoded before being returned.
add this:
header("Content-Type: application/json");
echo json_encode($result);